728x90
https://school.programmers.co.kr/learn/courses/30/lessons/250137
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
1. Java
class Solution {
public int solution(int[] bandage, int health, int[][] attacks) {
int answer = health;
int t = bandage[0];
int x = bandage[1];
int y = bandage[2];
for ( int i = 0; i < attacks.length; ++i ) {
int attack_time = attacks[i][0];
answer -= attacks[i][1];
if ( answer <= 0 ) {
answer = -1;
break;
}
if ( i == attacks.length-1 ) {
break;
}
int next_attack_time = attacks[i+1][0];
int recovery_time = next_attack_time - attack_time - 1;
answer = Math.min( health, answer + (recovery_time * x) );
if ( attack_time + t < next_attack_time ) {
answer = Math.min( health, answer + ( (int)(recovery_time / t) * y ) );
}
}
return answer;
}
}
2. Python
def solution(bandage, health, attacks):
answer = health
t = bandage[0]
x = bandage[1]
y = bandage[2]
for i in range(len(attacks)) :
attack_time = attacks[i][0]
answer -= attacks[i][1]
if answer <= 0 :
answer = -1
break
if i == len(attacks) - 1 :
break
next_attack_time = attacks[i+1][0]
recovery_time = next_attack_time - attack_time - 1
answer = min( health, answer + (recovery_time * x) )
if attack_time + t < next_attack_time :
answer = min( health, answer + ( recovery_time // t * y ) )
return answer
반응형
'코딩 문제 풀기 ( Algorithm problem solving ) > 프로그래머스 ( Programmers )' 카테고리의 다른 글
[프로그래머스(Programmers)][Java,Python] (Lv1) 동영상 재생기 (PCCP 기출문제 1번) (0) | 2025.03.26 |
---|---|
[프로그래머스(Programmers)][Java,Python] (Lv1) 공원 (PCCE 기출문제 10번) (0) | 2025.03.25 |
[프로그래머스(Programmers)][Java,Python] (Lv1) 유연근무제 (0) | 2025.03.25 |
[프로그래머스(Programmers)][Java,Python] (Lv1) 공원 산책 (0) | 2025.03.24 |
[프로그래머스(Programmers)][Java,Python] (Lv1) 달리기 경주 (0) | 2025.03.24 |