반응형
구현
PCCP로 코딩테스트 역량을 보는 필기 시험이 있어서, PCCP를 준비하며 문제를 풀어봤습니다.
https://programmers.co.kr/app/with_setting/tests/131575/challenges/algorithms/20179?language=python3
문제 풀이
def solution(bandage, health, attacks):
now_health = health # 최대 체력
attack_time = [i[0] for i in attacks]
dealing = [i[1] for i in attacks]
max_time = max([i[0] for i in attacks]) # 최대시간
bandage_time = bandage[0] # 시전 시간
second_healing = bandage[1] # 초당 회복량
more_healing = bandage[2] # 추가회복량
cnt = 0 # 붕대 시간
for time in range(1,max_time+1):
if time in attack_time: # 시간이 경과했을 때 공격 상태일 때
cnt = 0 #연속 성공 초기화
now_health -= dealing[attack_time.index(time)] #몬스터 공격
if now_health <= 0: # 몬스터 공격했을때 피가 0이하면 종료
answer = -1
return answer
# break
else:
cnt +=1
if cnt == bandage_time: # 시전 시간이 됐을 때
cnt = 0
now_health += more_healing
if now_health >= health :
now_health = health
if now_health == health: # 체력이 최대 체력일 때
pass
else: # 체력이 최대 체력이 아닐 때
now_health += second_healing
if now_health >= health:
now_health = health
print(now_health)
if now_health <= 0:
answer = -1
else:
answer = now_health
return answer
문제 해석
조건에 맞춰 코드를 작성했습니다.
1. 시간이 흐른다.
1 - 1) 몬스터한테 공격을 받았을 경우 피가 감소함
1 - 2) 몬스터 한테 공격 받고 피가 0 이하면 종료
2. 시간이 흐를 때 초당 체력힐링으로 피가 증가함.
2 - 1) 피는 최대 체력을 넘을 수 없음
2 - 2) 붕대 요구 시간이 다차면 추가 힐링을 시작함.
'Algorithm > programmers' 카테고리의 다른 글
프로그래머스 level3 경주로 건설 - python (2) | 2024.05.02 |
---|---|
프로그래머스 level2 전력망을 둘로 나누기 - python (4) | 2024.03.21 |
프로그래머스 level3 베스트앨범 - python (1) | 2024.01.08 |
프로그래머스 level2 의상 - python (2) | 2023.11.29 |
프로그래머스 level2 [2019 카카오 개발자 겨울 인턴십]튜플 - python (4) | 2023.11.23 |