문제
프로그래머스
SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
풀이
몬스터에게 공격을 당했을 경우와 공격을 당하지 않았을 경우를 생각하면 되는 문제이다.
1. 공격을 당한 도중에 체력이 0이하가 되면, -1을 리턴한다.
2. 공격을 할 수 있는 최대 시간보다 현재 시간이 더 클 경우, 체력이 0 이하 여부에 따라서 리턴한다.
3. 공격을 당했을 경우 현재 시간 값을 증가시키고, 붕대 감는 시간과 체력이 소모 처리를 해준다.
4. 공격을 당하지 않았을 경우, 체력을 증가시키고, 연속된 시간 여부에 따라서 추가적인 체력을 준다.
소스코드
class Solution {
public int solution(int[] bandage, int health, int[][] attacks) {
int currentHealth = health;
int increTime = bandage[0];
int healthAmount = bandage[1];
int addHealthAmount = bandage[2];
int maxAttackTime = attacks[attacks.length - 1][0];
int attackIndex = 0;
int currentTime = 0;
int successTime = 0;
while (true) {
//현재 체력이 모두 소진 되었으면 => 죽은거임
if (currentHealth <= 0) {
return -1;
}
if (currentTime > maxAttackTime) {
break;
}
//현재 시간이 공격 시간과 동일하다면,
if (currentTime == attacks[attackIndex][0]) {
//공격 당해서, 체력이 소모
currentHealth -= attacks[attackIndex][1];
//시간 흘려 보내기
currentTime += 1;
//붕대 감기 성공 시간 초기화
successTime = 0;
//다음 공격 인덱스 늘리기
attackIndex += 1;
continue;
}
//공격을 받지 않음
currentHealth += healthAmount;
successTime += 1;
//추가적인 체력을 얻어야할 때,
if (successTime == increTime) {
currentHealth += addHealthAmount;
successTime = 0;
}
//최대 체력 유지
if (currentHealth > health) {
currentHealth = health;
}
currentTime += 1;
}
return currentHealth <= 0 ? -1 : currentHealth;
}
}'Problem Solving > Programmers' 카테고리의 다른 글
| [Programmers] 왼쪽 오른쪽 - Java (0) | 2025.06.03 |
|---|---|
| [Programmers] H-Index - Java (1) | 2025.05.29 |
| [Programmers] 배열 만들기 2 - Java (0) | 2025.05.13 |
| [Programmers] 코드 처리하기 - Java (0) | 2025.05.11 |
| [Programmers] 배열 만들기 5 - Java (0) | 2025.05.10 |