카테고리 없음

[프로그래머스] 붕대감기

메리짱123 2024. 10. 19. 00:57
반응형

새로 써본것

Arrays.fill(array,value);

Arrays.fill(seconds, bandage[1]);

 

 

풀어본 방법 1

각 초에 대해 회복 action을 넣어뒀음.

int[] seconds를 1000개짜리로 초기화하니까 런타임에러가 났다. 너무 오래걸리나보다..

import java.util.*;

class Solution {
    public int solution(int[] bandage, int health, int[][] attacks) {
        int answer = 0;
        int lastSec = attacks[attacks.length-1][0];
        int[] seconds = new int[lastSec+1];      
        int plusHealth = bandage[2];
        Arrays.fill(seconds, bandage[1]); //기본회복량으로 fill
        for(int i=0; i< attacks.length ; i++){
            seconds[attacks[i][0]] =  attacks[i][1] * -1; //해당 초에 데미지 넣기
        }
        
        int count = 0;
        int max = health;
        for(int i=1;i<=lastSec;i++){
            if(seconds[i] < 0){
                //공격
                health += seconds[i];
                count = 0;
                if(health<=0) return -1;
            }else{
                health += seconds[i];
                count += 1;     
               
                if(count == bandage[0]){
                    health += plusHealth;
                    count = 0;
                }               
                //최대체력인지검사
                health = Math.min(max,health);
            }
        }
        return health <= 0 ? -1 : health;
    }
}

//각 초의 action
//공격을받거나
//회복스킬 count
반응형