https://school.programmers.co.kr/learn/courses/30/lessons/142085
function solution(n, k, enemy) {
var answer = 0;
const [arr, invincibility] = [enemy.reduce((acc, e) => ((acc[e] = ++acc[e] || 1), acc), []), {}];
for (let i = arr.length - 1, tempK = k; i >= 0 && tempK; --i) {
if (!arr[i]) continue;
if (tempK <= arr[i]) (invincibility[i] = tempK), (tempK = 0);
else (invincibility[i] = arr[i]), (tempK -= arr[i]);
}
for (e of enemy) {
if (invincibility[e] && k) {
--k, --invincibility[e], ++answer;
continue;
}
if (n < e && k) --k, ++answer;
else if (n >= e) (n -= e), ++answer;
else break;
}
return answer;
}
GitHub :
'Coding Test > Programmers' 카테고리의 다른 글
[Programmers] Lv 2. JadenCase 문자열 만들기 (0) | 2023.05.11 |
---|---|
[Programmers] Lv 2. 최댓값과 최솟값 (0) | 2023.05.10 |
[Programmers] (그리디. Greedy) Lv 2. 광물 캐기 (0) | 2023.04.18 |
[Programmers] (BFS) Lv 2. 미로 탈출 (0) | 2023.04.17 |
[Programmers] Lv 2. N-Queen (0) | 2023.04.10 |
댓글