https://school.programmers.co.kr/learn/courses/30/lessons/250137
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
- 나의 풀이
✅ 1초 부터 ~ 공격의 마지막 시간까지만 for문 반복 ➡︎ attacks[attackCount-1][0]
✅ 해시맵 attackMap 사용하여 몬스터 공격 시간 체크
- HashMap<Integer, Integer> attackMap = new HashMap<>(); ➡︎ < 공격시간(key), 피해량(value) >
- attackMap.containsKey(i) ➡︎ 공격 해시맵에 해당 시간이 있으면 몬스터 공격, 없으면 붕대 감기
- 나의 코드
import java.util.*;
class Solution {
//연속 성공 횟수
private static int count = 0;
//붕대감기
private static int bandaging(int health, int[] bandage, int maxHealth) {
count++;
health = health + bandage[1];
if(bandage[0] == count) {
health = health + bandage[2];
count = 0;
}
if(health > maxHealth) {
health = maxHealth;
}
return health;
}
//몬스터공격
private static int attaking(int health, int attack) {
//연속공격 초기화
count = 0;
health = health - attack;
return health;
}
public int solution(int[] bandage, int health, int[][] attacks) {
int maxHealth = health;
int attackCount = attacks.length;
//System.out.println(attacks.length);
HashMap<Integer, Integer> attackMap = new HashMap<>();
for(int[] attack : attacks) {
attackMap.put(attack[0], attack[1]);
}
for(int i=1; i<=attacks[attackCount-1][0]; i++) {
if(attackMap.containsKey(i)) {
health = attaking(health, attackMap.get(i));
} else {
health = bandaging(health, bandage, maxHealth);
}
if(health <= 0) {
return -1;
}
}
return health;
}
}
'공부 > 알고리즘' 카테고리의 다른 글
JadenCase 문자열 만들기 (0) | 2025.01.04 |
---|---|
부족한 금액 계산하기 (0) | 2025.01.04 |
PCCP 기출문제 > 동영상 재생기 (0) | 2024.11.18 |
2021 Dev-Matching: 웹 백엔드 개발자(상반기) > 다단계 칫솔 판매 (2) | 2024.11.09 |
2018 KAKAO BLIND RECRUITMENT > [1차] 프렌즈4블록 (0) | 2024.11.07 |