문제 : https://school.programmers.co.kr/learn/courses/30/lessons/250137?language=python3
나의 풀이
def attack(health, damage, max_health):
health -= damage
if health <= 0:
return -1, 0
return health, 0
def recover(health, count, bandage, max_health):
health += bandage[1]
count += 1
if count == bandage[0]:
health += bandage[2]
count = 0
health = min(health, max_health)
return health, count
def solution(bandage, health, attacks):
max_health = health
count = 0
attack_count = 0
time = 1
while time <= attacks[-1][0]:
if attack_count < len(attacks) and attacks[attack_count][0] == time:
health, count = attack(health, attacks[attack_count][1], max_health)
attack_count += 1
if health == -1:
return -1
else:
health, count = recover(health, count, bandage, max_health)
time += 1
return health
+ 함수 하나로 표현한 더 간단한 풀이
def solution(bandage, health, attacks):
max_health = health
count = 0
attack_count = 0
time = 1
while time <= attacks[-1][0]:
if attack_count < len(attacks) and time == attacks[attack_count][0]:
health -= attacks[attack_count][1]
count = 0
attack_count += 1
if health <= 0:
return -1
else:
health += bandage[1]
count += 1
if count == bandage[0]:
health += bandage[2]
count = 0
health = min(health, max_health)
time += 1
return health
'코딩테스트 > programmers (python)' 카테고리의 다른 글
Programmers / [PCCP 기출문제] 2번 / 석유 시추 / python (0) | 2025.02.18 |
---|---|
Programmers / [PCCP 모의고사 1회] 3번 / 유전법칙 / python (0) | 2025.02.14 |
Programmers / 2단계 / 행렬 테두리 회전하기 / python (0) | 2024.09.02 |
Programmers / 2단계 / 수식 최대 / python / 2020 카카오 인턴십 (0) | 2024.07.29 |
Programmers / 3단계 / 가장 먼 노드 / python (0) | 2024.07.26 |