본문 바로가기
Beakjoon&프로그래머스/Java

[프로그래머스/Java] [PCCP 기출문제] 1번 / 붕대 감기

by 현장 2026. 3. 20.

-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으로 처리하여 해결했습니다.