[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);
}
}
'๐ป Coding Problems Solving > Greedy' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ 8980] ํ๋ฐฐ (0) | 2023.06.15 |
---|---|
[BOJ 1092] ๋ฐฐ (0) | 2023.06.06 |
[ํ๋ก๊ทธ๋๋จธ์ค] ์๊ฒฉ์์คํ (์๋ฐ java) (0) | 2023.06.05 |
[BOJ 2212] ์ผ์ (0) | 2023.06.05 |
[BOJ 1700] ๋ฉํฐํญ ์ค์ผ์ค๋ง (java) (0) | 2023.04.05 |
์ต๊ทผ๋๊ธ