
-Code
class Solution {
public int solution(int[] bandage, int health, int[][] attacks) {
int answer = health;
int totalTime = attacks[attacks.length - 1][0];
int healTime = 0;
int attackIdx = 0;
for (int t = 0; t <= totalTime; t++) {
// 공격 확인
if (attacks[attackIdx][0] == t) {
// 공격이 존재하면 데미지 계산 및
// 연속 성공 초기화와 다음 공격으로 변경
answer -= attacks[attackIdx][1];
healTime = 0;
attackIdx++;
// 사망 처리
if (answer <= 0) {
return -1;
}
continue;
}
// 힐링 로직
healTime++;
answer += bandage[1];
// 연속 성공 횟수가 시전 시간만큼 성공하면 추가 회복
if (bandage[0] == healTime) {
answer += bandage[2];
healTime = 0;
}
// 풀피일때 회복된 경우 최대 hp량으로 맞춤
answer = Math.min(answer, health);
}
return answer;
}
}
사망 처리 및 hp 최대치 계산을 따로 if문으로 만들어 넣었다가 체력 처리에 문제가 생겨서 해당 부분을 Math.min으로 처리하여 해결했습니다.
'Beakjoon&프로그래머스 > Java' 카테고리의 다른 글
| [백준/Java] 34991번 toycppartoon (0) | 2026.03.21 |
|---|---|
| [백준/Java] 9842번 Prime (0) | 2026.03.21 |
| [프로그래머스/Java] [PCCE 기출문제] 10번 / 공원 (0) | 2026.03.20 |
| [백준/Java] 4365번 Stack 'em Up (0) | 2026.03.20 |
| [백준/Java] 18115번 Issuing Plates (0) | 2026.03.19 |