forked from GreatAlgorithm-Study/AlgorithmStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJY_142085.java
More file actions
31 lines (25 loc) · 835 Bytes
/
JY_142085.java
File metadata and controls
31 lines (25 loc) · 835 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
import java.util.*;
class JY_142085 {
public int solution(int n, int k, int[] enemy) {
int answer = enemy.length;
// 무적권 개수 == 적의 수이면 모든 라운드 진행 가능
if(k >= enemy.length) return answer;
PriorityQueue<Integer> pq = new PriorityQueue<>();
// 무적권 개수가 k보다 커지면 pq에서 가장 약한 라운드로 소모하기
int tmp = 0;
for(int i=0; i<enemy.length; i++) {
pq.add(enemy[i]);
tmp++;
if(tmp > k) {
int e = pq.poll();
if(n - e < 0) {
answer = i;
break;
}
n -= e;
tmp--;
}
}
return answer;
}
}