programmers/Lv 1

PCCP 기출문제 1번 붕대감기

Meluu_ 2024. 6. 27. 09:15

🧫 문제 분석

✔️ 출처

프로그래머스 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. 문제를 수학적으로 한 번 생각해 볼 필요가 있다.

'programmers > Lv 1' 카테고리의 다른 글

PCCP 기출문제 데이터 분석  (0) 2024.06.27
공원 산책  (0) 2024.06.27
추억 점수  (0) 2024.06.27
프로그래머스Lv. 1 로또의 최고 순위와 최저 순위  (0) 2024.06.27
프로그래머스Lv 1 옹알이 (2)  (0) 2024.06.27