-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathJW_142085.java
More file actions
25 lines (25 loc) ยท 1.09 KB
/
JW_142085.java
File metadata and controls
25 lines (25 loc) ยท 1.09 KB
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
import java.util.PriorityQueue;
class JW_142085 {
public int solution(int n, int k, int[] enemy) {
// ๋ฌด์ ๊ถ์ ์ฌ์ฉํ ๋ ๊ทธ๋ฆฌ๋ํ ์ ํ์ ํ๊ธฐ ์ํ ์ฐ์ ์์ ํ
PriorityQueue<Integer> pq = new PriorityQueue<>((o1, o2) -> o2 - o1);
int round = 0;
int len = enemy.length;
for (; round < len; round++) {
// ๋ผ์ด๋๋ง๋ค ์ฐ์ ์์ ํ์ ์ง์ด๋ฃ์
pq.offer(enemy[round]);
// ๋ง์ฝ ํด๋น ๋ผ์ด๋์ ๋ชฌ์คํฐ๋ฅผ ๋ฌด์ฐ๋ฅผ ์ ์์ ๊ฒฝ์ฐ
if (n < enemy[round]) {
// ๋ฌด์ ๊ถ์ด ์๋ค๋ฉด ๊ฒ์ ์ข
๋ฃ
if (0 == k)
break;
// ํ์ฌ ์ฐ์ ์์ ํ์ ๋ค์ด๊ฐ ์๋ ๊ฐ๋ค ์ค
// ์ต๋๊ฐ์ ๊บผ๋ด ํด๋น ๋ผ์ด๋ ๋ฌด์ ๊ถ ์ฌ์ฉ
n += pq.poll(); // ํด๋น ๋ผ์ด๋์ ๋ชฌ์คํฐ ์๋ฅผ ๋ค์ ๋ํด์ค
k--; // ๋ฌด์ ๊ถ ๊ฐ์
}
n -= enemy[round]; // ํด๋น ๋ผ์ด๋์ ๋ชฌ์คํฐ ์ ๋งํผ ๋ณ์ฌ ๊ฐ์
}
return round;
}
}