[PCCP 기출문제] 1번 / 붕대 감기 자바

Heeeoh·2024년 3월 19일
0

프로그래머스

목록 보기
21/26
post-thumbnail

🧫 문제 분석

✔️ 출처

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

📖 문제


🔅 문제 풀이

class Solution {
    public static int solution(int[] bandage, int health, int[][] attacks) {
        int time = 0;
        int timeIndex = 0;
        int healthTime = 0;
        int maxHp = health;

        //공격 시간 
        int attackTime = attacks[timeIndex][0];

        while(health > 0) {
            time++;

            if (timeIndex == attacks.length ) {
                return health;
            }
            // 공격
            if (time == attackTime) {
                health -= attacks[timeIndex++][1];
                if (timeIndex != attacks.length) {
                    attackTime = attacks[timeIndex][0];
                }
                healthTime = 0;

                // 회복
            } else {
                if (maxHp > health + bandage[1]) {
                    health += bandage[1];
                    healthTime++;
                } else {
                    health = maxHp;
                }

                if (healthTime == bandage[0]) {
                    healthTime = 0;

                    // 추가 회복량 
                    if (maxHp >= health + bandage[2]) {
                        health += bandage[2];
                    } else {
                        health = maxHp;
                    }
                }
            }

        }
        return -1;

    }
}

문제를 제대로 이해하지 못해서 좀 애먹었다. 핵심은 회복량이 최대 체력을 넘길양이면 최대체력까지만 회복하고 더이상 회복하지 않는다는 것이다..

❗ 오답노트 / 필요한 지식

  1. Math 클래스를 잘 이용해보자.
  2. 문제를 수학적으로 한 번 생각해 볼 필요가 있다.
profile
열심히 살자

0개의 댓글