코딩테스트/자바 코딩테스트

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

Samsun 2024. 4. 15. 00:22
반응형
import java.util.*;
class Solution {
    public int solution(int[] bandage, int health, int[][] attacks) {
        Map<Integer, Integer> attackInfo = new HashMap<>();
        int now_hp = health;
        int successcount = 0;
        
        for(int[] attack : attacks) {
            attackInfo.put(attack[0], attack[1]);
        }
        
        for(int i=1; i<=attacks[attacks.length-1][0]; i++){
            if(attackInfo.containsKey(i)){
                now_hp -= attackInfo.get(i);
                successcount = 0;
            }
            else {
                now_hp+=bandage[1];
                successcount++;
                if(successcount == bandage[0]){
                    now_hp += bandage[2];
                    successcount = 0;
                }
                if(now_hp > health) {
                    now_hp = health;
                }
                
            }
            if(now_hp <= 0){
                    return -1;
                }
        }
        
        return now_hp;
    }
}