728x90
붕대 감기
https://school.programmers.co.kr/learn/courses/30/lessons/250137



풀이
공격이 있는 경우
if (attackIdx < attacks.length && time == attacks[attackIdx][0]) {
health -= attacks[attackIdx][1];
cnt = 0;
attackIdx++;
if (health <= 0) return -1;
}
공격이 없는 경우 -> 회복
else {
cnt++;
health += bandage[1];
if (cnt == bandage[0]) {
health += bandage[2];
cnt = 0;
}
if (health > maxHealth) health = maxHealth;
}
코드
class Solution {
public int solution(int[] bandage, int health, int[][] attacks) {
int maxHealth = health;
int time = 1;
int cnt = 0;
int attackIdx = 0;
int endTime = attacks[attacks.length - 1][0];
while (time <= endTime) {
if (attackIdx < attacks.length && time == attacks[attackIdx][0]) {
health -= attacks[attackIdx][1];
cnt = 0;
attackIdx++;
if (health <= 0) return -1;
} else {
cnt++;
health += bandage[1];
if (cnt == bandage[0]) {
health += bandage[2];
cnt = 0;
}
if (health > maxHealth) health = maxHealth;
}
time++;
}
return health;
}
}728x90
반응형
'✏️ > Programmers' 카테고리의 다른 글
| [프로그래머스/Lv.1] 키패드 누르기 (Java) (0) | 2026.02.04 |
|---|---|
| [프로그래머스/Lv.1] 문자열 내 마음대로 정렬하기 (Java) (0) | 2026.02.03 |
| [프로그래머스/Lv.1] PCCP - 동영상 재생기 (Java) (0) | 2026.02.03 |
| [프로그래머스/Lv.2] 메뉴 리뉴얼 (Java) (0) | 2026.02.01 |
| [프로그래머스/Lv.2] 오픈채팅방 (Java) (0) | 2026.02.01 |