-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSB_142085.java
More file actions
20 lines (17 loc) ยท 844 Bytes
/
SB_142085.java
File metadata and controls
20 lines (17 loc) ยท 844 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.util.*;
public class SB_142085 {
public int solution(int n, int k, int[] enemy) {
PriorityQueue<Integer> pq = new PriorityQueue<>();
int N = enemy.length;
if(k>=N) return N; // ๋ค ๋ฌด์กฐ๊ฑด์ผ๋ก ์ด๊ธธ ์ ์์ ๊ฒฝ์ฐ
for(int i=0; i<N; i++){
pq.offer(enemy[i]); // ํ์ ํ์ฌ ๋ผ์ด๋ ์ถ๊ฐ
if(pq.size() > k){ // ํ์ ํฌ๊ธฐ๊ฐ k๋ณด๋ค ํฌ๋ฉด, ๋ฌด์กฐ๊ฑด ์ฌ์ฉ ๋ถ๊ฐ๋ก
int weak = pq.poll(); // ์ ์ผ ์ฝํ์ ๋ ์ธ์ฐ๊ธฐ
if(n-weak < 0) return i; // i๋ ํ์ฌ๋ผ์ด๋-1 ๊ฐ์ ๋ํ๋ด๊ธฐ์ ํ์ฌ ๋ผ์ด๋์์ ์ง๋ฉด i๋ฐํ
n-=weak; // ์ธ์ฐ๊ณ ๋ณ์ฌ ์ ์ค๊ธฐ
}
}
return N;
}
}