본문 바로가기
백준 & 프로그래머스

프로그래머스.붕대감기.java

by concho 2023. 11. 28.
import java.util.*;
import java.io.*;

// defalt bandage[1] 만큼 회복 else -공격
// bandage[0]초 => bandage[2] 추가 회복
// [시전 시간, 초당 회복량, 추가 회복량] 최대 100
// [공격 시간, 피해량]  이미 정렬
class Solution{
    public int solution(int[] bandage, int health, int[][] attacks){
        int attackCnt = 0,heelCnt = 0;
        int firstAttack = attacks[0][0];
        int lastAttack = attacks[attacks.length - 1][0];
        int maxHealth = health;

        for(int t = firstAttack; t <= lastAttack; t++){
            if(t == attacks[attackCnt][0]){
                health -= attacks[attackCnt][1];
                attackCnt++; 
                heelCnt = 0;
            }
            else{
                heelCnt++;
                health += bandage[1];
                if(heelCnt == bandage[0]){
                    health += bandage[2];
                    heelCnt = 0;
                }
                if (health > maxHealth) health = maxHealth;
            }
            if(health <= 0) return -1;
        }
        return health;
    }

    public static void main(String[] args){
        Solution sol = new Solution();

        // 예제 데이터
        int[] bandage = {1, 1, 1};
        int health = 5;
        int[][] attacks = {{1,2}, {3,2}};

        int result = sol.solution(bandage, health, attacks);
        System.out.println("Result: " + result);
    }
}

 

댓글