[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);
    }

}
  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ