Notice
Recent Posts
Recent Comments
250x250
Creative Code
붕대 감기 본문
728x90
https://school.programmers.co.kr/learn/courses/30/lessons/250137
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
int solution(vector<int> bandage, int health, vector<vector<int>> attacks) {
int answer = 0;
int success = 0; // 연속 성공시간
int status_health = health; // 현재체력
int timeend = attacks[attacks.size() - 1][0]; //몬스터의 마지막 공격시간
int index = 0; // attacks 탐색 index
for (int i = 1; i <= timeend; i++) {
if (i == attacks[index][0]) { // 몬스터가 공격할 때
status_health -= attacks[index][1];
if (status_health <= 0) { // 캐릭터가 사망 시 -1을 리턴
return -1;
}
index++; // 캐릭터가 안죽었을 때 다음 index로 변경
success = 0; // 캐릭터가 공격받을 시 연속 회복시간 초기화
}
else { // 몬스터가 공격받지 않아 회복할 때
success += 1; // 연속 회복성공 시간 1초 추가
status_health += bandage[1]; // 체력 회복
if (success == bandage[0]) { // 연속 회복 모두 성공시 추가회복
status_health += bandage[2];
success = 0;
}
if (status_health >= health) { // 회복후 체력이 최대체력보다 많을시 최대체력으로 변경
status_health = health;
}
}
}
return status_health;
}
728x90
'프로그래머스 코테 문제 > LEVEL 1' 카테고리의 다른 글
가장 많이 받은 선물 (0) | 2024.04.12 |
---|---|
데이터 분석 (0) | 2024.04.12 |
이웃한 칸 (0) | 2024.04.12 |