[BOJ 13164] ํ–‰๋ณต์œ ์น˜์›

 

1. ๋ฌธ์ œ : https://www.acmicpc.net/problem/13164

 

2. ํ’€์ด

๋‘ ์ˆ˜์˜ ์ฐจ์ด๊ฐ€ ๋งŽ์ด ๋‚˜๋Š” ๊ณณ์— ๋ง‰๋Œ€๋ฅผ ๋‘๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ทธ๋ฆฌ๋””ํ•˜๊ฒŒ ํ‘ธ๋Š” ๋ฌธ์ œ

 

3. ์ฝ”๋“œ

package baekjoon;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class ํ–‰๋ณต์œ ์น˜์› {

    static int N, K;
    static int arr[];
    static int answer = 1000000000;


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int N = sc.nextInt();
        int K = sc.nextInt();
        int answer = 0;
        ArrayList<Integer> tall = new ArrayList<>();
        ArrayList<Integer> subTall = new ArrayList<>();

        //ํ‚ค ์ž…๋ ฅ๋ฐ›๊ธฐ
        for(int i = 0; i<N; i++){
            tall.add(sc.nextInt());
        }
        //ํ‚ค ์ฐจ์ด ๊ตฌํ•˜๊ธฐ
        for(int i = 0; i < N-1; i++){
            subTall.add(tall.get(i+1) - tall.get(i));
        }
        //ํ‚ค ์ฐจ์ด ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
        Collections.sort(subTall);

        //๊ฐ€์žฅ ํฐ ํ‚ค์ฐจ์ด๋ฅผ K-1๊ฐœ ์ œ์™ธํ•˜๊ณ  ๋‚˜๋จธ์ง€๋“ค ๋”ํ•จ
        for(int i = 0; i < N-K; i++){
            answer += subTall.get(i);
        }
        System.out.println(answer);
    }

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