-
Notifications
You must be signed in to change notification settings - Fork 4
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;
}
}