문제
https://school.programmers.co.kr/learn/courses/30/lessons/250137
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
정답
import java.util.*;
class Solution {
public int solution(int[] bandage, int health, int[][] attacks) {
int chain = 0;
int maxHealth = health;
//공격기록을 맵으로 기록
int lastAttack = attacks.length;
HashMap<Integer,Integer> attackLog = new HashMap<>();
for(int[] attack : attacks){
attackLog.put(attack[0],attack[1]);
}
//공격이록이 존재하는 초와 아닌 초로 나누어 로직 생성
for(int i=0;i<=attacks[lastAttack-1][0];i++){
//공격을 받았을때
if(attackLog.get(i)!=null){
chain=0;
health-=attackLog.get(i);
if(health<=0){
return -1;
}
continue;
}else{
//chain이 꽉차지않았을땐 회복량만 꽉차면 체력 추가회복 후 초기화
chain++;
if(chain<bandage[0]){
health+=bandage[1];
}else if(chain==bandage[0]){
health+=bandage[1];
health+=bandage[2];
chain=0;
}
//최대체력을 넘어갈 경우 최대체력으로 복귀
if(health>maxHealth){
health=maxHealth;
}
}
}
return health;
}
}
연속적인 행동이 일어났을때의 상황이 실수를 하기 좋아보인다.
게임을 연관지어 만드니 문제가 재밌었다.
'Coding Test > JAVA 코딩테스트 풀이정리(프로그래머스)' 카테고리의 다른 글
프로그래머스 스쿨 Lv.1 - 시저 암호(아스키값 / stringBuilder) (1) | 2024.01.26 |
---|---|
프로그래머스 스쿨 Lv.1 - [PCCE 기출문제] 10번 / 데이터 분석 (0) | 2024.01.09 |
프로그래머스 스쿨 Lv.1 - 콜라 문제(조건에 맞는 반복문) (0) | 2024.01.07 |
프로그래머스 스쿨 Lv.1 - 로또의 최고 순위와 최저 순위(등수 예외 처리) (1) | 2024.01.07 |
프로그래머스 스쿨 Lv.1 - 문자열 나누기(String.split) (0) | 2024.01.07 |