코딩테스트 연습 > 붕대 감기
https://school.programmers.co.kr/learn/courses/30/lessons/250137
t초 마다 붕대를 감으면 1초마다 x만큼의 체력을 회복, t초 연속으로 붕대 감으면 y만큼의 추가 회복.
최대 체력보다 더 회복할 수 없음.
몬스터의 공격을 당하면 기술이 취소 되어, 연속 성공 시간이 0으로 초기화 + 붕대 감기를 다시 사용해야한다.
시전 시간, 1초당 회복량, 추가 회복량을 담은 정수 배열,
최대 체력 정수,
몬스터의 공격 시간과 피해량을 담은 정수 배열이 주어질 때,
모든 공격이 끝난 직후 남은 체력을 return하라.
단, 캐릭터의 체력이 0이 되는 순간 -1 return 한다.

몬스터의 다음 공격시간과 공격의 데미지를 저장하고, 현재 시간으로부터 다음 공격시간의 차이를 계산하여 이를 duration으로 저장한다.
duration 동안 초당 회복을 진행하고, duration을 시전시간 만큼 나누게 되면, 추가 회복량을 얼마나 할 수 있는지 알 수 있다.
이를 이용하여 추가 회복까지 진행한다.
추가 회복까지 진행한 후, 데미지를 계산한다.
데미지를 받는 동안 힐을 할 수 없으므로 해당 시간에서는 힐을 제외하여 데미지만 계산한다.
이후, 현재 시간을 데미지 입은 시간으로 초기화한다.
class Solution {
public int solution(int[] bandage, int health, int[][] attacks) {
int maxHealth = health;
int curHealth = health;
int curTime = 0;
int combo = 0;
for (int i = 0; i < attacks.length; i++) {
int nextAttackTime = attacks[i][0];
int damage = attacks[i][1];
int duration = nextAttackTime - curTime - 1;
if (duration > 0) {
curHealth += bandage[1] * duration;
combo += duration;
int fullComboCount = combo / bandage[0];
curHealth += fullComboCount * bandage[2];
if (curHealth > maxHealth) {
curHealth = maxHealth;
}
}
curHealth -= damage;
if (curHealth <= 0) return -1;
combo = 0;
curTime = nextAttackTime;
}
return curHealth;
}
}
Review
class Solution {
public int solution(int[] bandage, int health, int[][] attacks) {
int curTime = 0;
int maxHealth = health;
int healTime = 0;
int comboHealTime = 0;
for(int i = 0; i<attacks.length; i++){
healTime = attacks[i][0] - curTime - 1;
comboHealTime = healTime / bandage[0];
maxHealth += (healTime * bandage[1]) + (comboHealTime * bandage[2]);
if(maxHealth > health) maxHealth = health;
maxHealth -= attacks[i][1];
if(maxHealth <= 0) return -1;
comboHealTime = 0;
curTime = attacks[i][0];
}
return maxHealth;
}
}
좀 더 차근차근 경우를 나누면 쉽게 풀었을 것 같다.
Review
맨 마지막에 curTime을 공격시간으로 초기화해야했는데, 초기화하지 않아서 처음에 틀렸었다.


Review
