Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- @NoArgsConstructor
- 슈더코드
- 백준
- this
- interrupted()
- 생성자
- 백준 11660번
- Bean LifecCycle
- json
- 백준 1235번
- this와 this() 차이
- 백준 11659번
- MariaDB Query Log
- ajax
- 자바 람다식
- InterruptException
- 합배열
- SQL
- select
- pseudo-code
- 마리아DB 쿼리 로그
- function test
- 상속과 참조
- map()
- 구간합구하기
- 구간합
- @AllArgsConstructor
- jquery
- Java
- 2차원배열 구간합
Archives
- Today
- Total
평범한 연구소
[프로그래머스] 붕대 감기 (JAVA) 본문
문제는 여기 ! https://school.programmers.co.kr/learn/courses/30/lessons/250137
attacks 2차원 배열을 해시맵에 담아서 꺼내쓰는 방향으로 리팩토링을 해봐야겠다.
private final int MAXIMUM_HEALTH = 30;
private boolean isAttacked = false;
private int continuousSuccess = 0;
public int solution(int[] bandage, int health, int[][] attacks) {
int attackCount = attacks[attacks.length-1][0]; // attack 2차배열 마지막요소의 y값
for (int i=1; i<=attackCount; i++) {
health -= getAttack(i, attacks);
health += getHeal(health, bandage);
if(isDead(health)) return -1;
}
return health;
}
private int getAttack(int count, int[][] attacks) {
int targetAttack = 0;
for (int i=0; i<attacks.length; i++) {
if(attacks[i][0] == count) targetAttack =attacks[i][1];
}
if (targetAttack > 0) attack();
else nonAttack();
return targetAttack;
}
private int getHeal(int currnetHealth, int[] bandage) {
if(isAttacked) return 0;
int bonusCount = bandage[0];
int healToPer = bandage[1];
int healThenContinuous = bandage[2];
int result;
if(continuousSuccess>=bonusCount) result = healToPer + healThenContinuous;
else result = healToPer;
if(currnetHealth >= MAXIMUM_HEALTH) {
return 0;
} else {
int total = currnetHealth+result;
return total>=30 ? result-(total-30) : result;
}
}
private boolean isDead(int health) {
return health<=0;
}
private void nonAttack() {
isAttacked = false;
continuousSuccess++;
}
private void attack() {
isAttacked = true;
continuousSuccess = 0;
}
'JAVA > 알고리즘 공부' 카테고리의 다른 글
[JAVA] 구간 합 구하기 5 - 백준 11660번 (0) | 2025.03.08 |
---|---|
[JAVA] 백준 11659번: 구간 합 구하기 (0) | 2025.03.06 |
[JAVA] 유닉스 timestamp → Date, String으로 바꾸기 (unix timestamp to String) (0) | 2022.12.30 |
[JAVA] 에라토스테네스의 체 (0) | 2022.12.17 |
[백준] 17103번: 골드바흐 파티션 (자바 JAVA) (0) | 2022.12.17 |