[BOJ 2212] ์ผ์
1. ๋ฌธ์ : https://www.acmicpc.net/problem/2212
2. ํ์ด
๊ทธ๋ฆฌ๋ํ๊ฒ ์ฐจ์ด๊ฐ ์ ๊ฒ๋๋ ๋ถ๋ถ ๋ํด์ฃผ๊ธฐ
3. ์ฝ๋
package baekjoon;
import java.util.*;
public class Main {
static int N,K;
static int cnt;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
K = sc.nextInt();
List<Integer> arr = new ArrayList<>();
List<Integer> temp = new ArrayList<>();
int now;
for(int i=0; i<N; i++){
now = sc.nextInt();
if(arr.contains(now)){
continue;
}else{
arr.add(now);
}
}
Collections.sort(arr);
int len = arr.size();
int a;
for(int i=1; i<len; i++){
a = arr.get(i) - arr.get(i-1);
temp.add(a);
}
Collections.sort(temp);
for(int i=0; i<len-K; i++){
cnt += temp.get(i);
}
System.out.println(cnt);
}
}
'๐ป Coding Problems Solving > Greedy' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ 8980] ํ๋ฐฐ (0) | 2023.06.15 |
---|---|
[BOJ 1092] ๋ฐฐ (0) | 2023.06.06 |
[ํ๋ก๊ทธ๋๋จธ์ค] ์๊ฒฉ์์คํ (์๋ฐ java) (0) | 2023.06.05 |
[BOJ 13164] ํ๋ณต์ ์น์ (0) | 2023.05.30 |
[BOJ 1700] ๋ฉํฐํญ ์ค์ผ์ค๋ง (java) (0) | 2023.04.05 |
์ต๊ทผ๋๊ธ