์ƒˆ์†Œ์‹

์•Œ๊ณ ๋ฆฌ์ฆ˜/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋ถ•๋Œ€ ๊ฐ๊ธฐ

  • -
728x90

 

1. ๋ฌธ์ œ



bandage : {๊ธฐ์ˆ ์˜ ์‹œ์ „ ์‹œ๊ฐ„, 1์ดˆ๋‹น ํšŒ๋ณต๋Ÿ‰,์ถ”๊ฐ€ ํšŒ๋ณต๋Ÿ‰}
health : ์ตœ๋Œ€ ์ฒด๋ ฅ
attacks : {๋ชฌ์Šคํ„ฐ์˜ ๊ณต๊ฒฉ ์‹œ๊ฐ„, ํ”ผํ•ด๋Ÿ‰}

Return : ๋ชจ๋“  ๊ณต๊ฒฉ์ด ๋๋‚œ ์งํ›„ ๋‚จ์€ ์ฒด๋ ฅ, ๋งŒ์•ฝ ์ฒด๋ ฅ์ด 0 ์ดํ•˜๋ฉด -1 ์ถœ๋ ฅ


2. ์ฝ”๋“œ

#include <string>
#include <vector>
#include <iostream>

using namespace std;

/*

Return
- ๋ชจ๋“  ๊ณต๊ฒฉ์ด ๋๋‚œ ์งํ›„ ๋‚จ์€ ์ฒด๋ ฅ
- ๋งŒ์•ฝ ์ฒด๋ ฅ์ด 0 ์ดํ•˜๊ฐ€ ๋˜์–ด ์ฃฝ์œผ๋ฉด -1

Rule
- ์ถ”๊ฐ€ ํšŒ๋ณต๋Ÿ‰ : bandage
- ์ตœ๋Œ€ ์ฒด๋ ฅ : health
- ๋ชฌ์Šคํ„ฐ์˜ ๊ณต๊ฒฉ ์‹œ๊ฐ„๊ณผ ํ”ผํ•ด๋Ÿ‰ : attacks
- t ์ดˆ ๋™์•ˆ 1์ดˆ๋งˆ๋‹ค x ๋งŒํผ์˜ ์ฒด๋ ฅ ํšŒ๋ณต
- t ์ดˆ ์—ฐ์†์œผ๋กœ ์„ฑ๊ณตํ•˜๋ฉด y๋งŒํผ ์ฒด๋ ฅ์„ ์ถ”๊ฐ€๋กœ ํšŒ๋ณต

*/

void heal(int& healStat,int healValue,int healthMax){
    healStat+=healValue;
    if(healStat>healthMax) healStat = healthMax;    
}

int solution(vector<int> bandage, int health, vector<vector<int>> attacks) {
    int answer = health;
    
    int endTime = attacks[attacks.size()-1][0];
    int combo=0,attackIdx=0;
    for(int i=0;i<=endTime;i++){
        if(attacks[attackIdx][0]==i){
            answer-=attacks[attackIdx][1];
            combo=0;
            attackIdx++;
        }
        else{
            combo++;
            heal(answer,bandage[1],health);
            if(combo==bandage[0]){
                //active combo
                heal(answer,bandage[2],health);
                combo=0;
            }
        }
        
        if(answer<=0){
            answer=-1;
            break;
        }
    }
    
    return answer;
}


3. ํ•ด์„ค

	 int endTime = attacks[attacks.size()-1][0];
    int combo=0,attackIdx=0;
    for(int i=0;i<=endTime;i++){
        if(attacks[attackIdx][0]==i){
            //cout<<"attackIdx: "<<attackIdx<<endl;
            answer-=attacks[attackIdx][1];
            combo=0;
            attackIdx++;
        }
        else{
            combo++;
            heal(answer,bandage[1],health);
            if(combo==bandage[0]){
                //active combo
                heal(answer,bandage[2],health);
                combo=0;
            }
        }
        
        if(answer<=0){
            answer=-1;
            break;
        }
        //cout<<"health: "<<health<<endl;
    }

endTime ์€ attacks ๋ฐฐ์—ด์˜ ๋งจ ๋งˆ์ง€๋ง‰ ์ธ์ž์˜ ๊ณต๊ฒฉ ์‹œ๊ฐ„์„ ๋ฐ›์•„์™”์Šต๋‹ˆ๋‹ค.
attacks๋Š” ์‹œ๊ฐ„์ˆœ์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ์ด ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์žฌ์ •๋ ฌํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.
๋ฐ›์•„์˜จ ๋งˆ์ง€๋ง‰ ๊ณต๊ฒฉ ์‹œ๊ฐ„๊นŒ์ง€ ์ฒดํฌ๋ฅผ ํ•˜๋Š” for๋ฌธ์„ ์ž‘๋™์‹œํ‚ต๋‹ˆ๋‹ค.
combo ๋Š” ์ฝค๋ณด๋ฅผ ๋ฐ›์•„์˜ฌ ๋ณ€์ˆ˜์ด๊ณ  attackIdx๋Š” attacks ๊ณต๊ฒฉํ•  ๋ฐฐ์—ด์˜ ์ˆœ์„œ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
attackIdx๋Š” ๊ณต๊ฒฉ์„ ๋ฐ›์„ ๋•Œ, ๋‹ค์Œ ์ธ์ž๋กœ ๋„˜์–ด๊ฐ€๋„๋ก ์„ค๊ณ„๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

if(attacks[attackIdx][0]==i)

์œ„์˜ if๋ฌธ์€ attackIdx ์ˆœ์„œ์— ๋”ฐ๋ฅธ ๊ณต๊ฒฉ ์ˆœ์„œ๊ฐ€ i(์‹œ๊ฐ„)๊ณผ ๊ฐ™์€์ง€ ์ฒดํฌ ํ•˜๋Š” ๊ตฌ๋ฌธ์ž…๋‹ˆ๋‹ค.
๋งŒ์•ฝ, attacks ๋ฐฐ์—ด์ด {{1,5},{3,10}} ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค๋ฉด
- ์‹œ๊ฐ„ 1์ผ ๋•Œ 5๋ฅผ ๊ณต๊ฒฉํ•œ๋‹ค.
- ์‹œ๊ฐ„ 3์ผ ๋•Œ 10์„ ๊ณต๊ฒฉํ•œ๋‹ค.
์ž…๋‹ˆ๋‹ค.
๋งŒ์•ฝ attackIdx ๊ฐ€ 0 ์ด๋ฉด, ์‹œ๊ฐ„ 1์ผ ๋•Œ ๊ณต๊ฒฉ์„ ์‹œ์ „ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

answer-=attacks[attackIdx][1];
combo=0;
attackIdx++;

๋งŒ์•ฝ i(์‹œ๊ฐ„)์ด ๋งž๋‹ค๋ฉด, ๊ณต๊ฒฉ์ด ์‹œ์ „๋ฉ๋‹ˆ๋‹ค.
๋”ฐ๋ผ์„œ answer(ํ˜„์žฌ ์ฒด๋ ฅ)์„ attacks[attackIdx][1](ํ”ผํ•ด๋Ÿ‰)๋งŒํผ ๊ฐ์†Œ์‹œํ‚ต๋‹ˆ๋‹ค.
์ฝค๋ณด๋Š” ๊นจ์ง€๊ธฐ ๋•Œ๋ฌธ์— 0์œผ๋กœ ๋ฐ”๊พธ์–ด์ค๋‹ˆ๋‹ค.
ํ•ด๋‹น ๊ณต๊ฒฉ์„ ๋ฐ›๊ณ  ๋‚˜์„œ๋Š” ๋‹ค์Œ ๊ณต๊ฒฉ์„ ์ฒดํฌํ•  ์ˆ˜ ์žˆ๋„๋ก attackIdx๋ฅผ ์ฆ๊ฐ€์‹œ์ผœ์ค๋‹ˆ๋‹ค.

else{
	combo++;
	heal(answer,bandage[1],health);
	if(combo==bandage[0]){
		//active combo
		heal(answer,bandage[2],health);
		combo=0;
	}
}

๋งŒ์•ฝ ๊ณต๊ฒฉ์„ ๋ฐ›์ง€ ์•Š๋Š”๋‹ค๋ฉด ์น˜์œ ๋ฅผ ํ•ฉ๋‹ˆ๋‹ค.
์ฝค๋ณด์˜ ๊ฐฏ์ˆ˜๋ฅผ ๋Š˜๋ฆฌ๊ณ , ์น˜์œ ๋ฅผ ํ•ฉ๋‹ˆ๋‹ค.

void heal(int& healStat,int healValue,int healthMax){
    healStat+=healValue;
    if(healStat>healthMax) healStat = healthMax;    
}

ํž์€ healthMax(์ตœ๋Œ€ ์ฒด๋ ฅ)์„ ๋„˜์–ด๊ฐ€๋ฉด ๋‹ค์‹œ ์กฐ์ •๋ฉ๋‹ˆ๋‹ค.

if(combo==bandage[0]){
	//active combo
	heal(answer,bandage[2],health);
	combo=0;
}

๋งŒ์•ฝ ์ฝค๋ณด๋ฅผ ๋‹ฌ์„ฑํ•˜๋ฉด ์ถ”๊ฐ€ ์ฒด๋ ฅ์ด ์น˜์œ ๋˜๋„๋ก ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

if(answer<=0){
	answer=-1;
	break;
}

๋งŒ์•ฝ ์ฒด๋ ฅ์ด 0์ดํ•˜๊ฐ€ ๋œ๋‹ค๋ฉด for๋ฌธ์„ ๋ฒ—์–ด๋‚˜๊ณ  -1์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.









๋ฐ˜์‘ํ˜•
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.