프로그래머스/1레벨
[프로그래머스/C++] [PCCP 기출문제] 1번 / 붕대 감기
Koalitsiya
2024. 1. 9. 16:45
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
1. 일단 모든 공격이 끝난 직후의 체력을 리턴하기 때문에 마지막 공격의 시전 시간을 구함
2. lastAttackTime까지 1초 단위로 반복한다고 가정하고 공격이 들어온다면 체력을 감소시키고 붕대 감는 시간을 초기화 시킴
3. 그리고 매 초 붕대를 감으며 현재 체력이 최대 체력이 넘어서면 현재 체력을 최대 체력으로 변경 시키고 붕대 감는 시간을 1씩 증가
4. 붕대를 완전히 감는데 성공했다면 bandage[2]만큼 체력을 회복하고 위 3번과 마찬가지로 현재 체력이 최대 체력을 넘어서지 않도록 함
5. 마지막 공격이 끝난 직후의 체력을 리턴
#include <string>
#include <vector>
using namespace std;
int solution(vector<int> bandage, int health, vector<vector<int>> attacks) {
int completeTime = 0;
int curHealth = health;
int lastAttackTime = attacks[attacks.size() - 1][0];
for(int i = 0, idx = 0; i <= lastAttackTime; i++){
if(attacks[idx][0] == i){
curHealth -= attacks[idx++][1];
if(curHealth <= 0) return -1;
completeTime = 0;
continue;
}
curHealth = min(curHealth + bandage[1], health);
completeTime++;
if(completeTime == bandage[0]){
curHealth = min(curHealth + bandage[2], health);
completeTime = 0;
}
}
return curHealth;
}