-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDH_142085.java
More file actions
40 lines (31 loc) · 900 Bytes
/
DH_142085.java
File metadata and controls
40 lines (31 loc) · 900 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.io.*;
import java.util.*;
/*
* 디펜스 게임
*/
public class DH_142085 {
public static int solution(int n, int k, int[] enemy) {
int answer = 0;
PriorityQueue<Integer> q = new PriorityQueue<Integer>(Collections.reverseOrder());
int sum = 0;
// 큐에 하나씩 적들을 담으면서, n보다 적들의 합이 더 커진다면
// 큐에서 가장 많은 적들을 꺼내서 무적권을 사용하기
for(int i = 0; i < enemy.length; i++) {
sum += enemy[i];
q.add(enemy[i]);
if(n < sum && k > 0) {
int diff = q.poll();
sum -= diff;
k -= 1;
}
if(n < sum) break;
answer = i + 1;
}
return answer;
}
public static void main(String[] args) {
int n = 2, k = 4;
int[] enemy = {3, 3, 3, 3};
System.out.println(solution(n, k, enemy));
}
}