[프로그래머스] PCCP 기출 1번 붕대감기 - 파이썬

JinUk Lee·2023년 11월 15일
0

프로그래머스

목록 보기
44/47

https://school.programmers.co.kr/learn/courses/19344/lessons/242258?language=python3



def solution(bandage, health, attacks):

    maxhealth = health
    maxtime = attacks[-1][0]
    attack_dict = {}
    for i in attacks:
        attack_dict[i[0]]=i[1]

    t=0

    continue_sec = 0

    while t<=maxtime:

        if t in attack_dict:
            health -= attack_dict[t]
            continue_sec=0

            # 공격을 맞고 체력이 -1
            if health <=0:
                return -1

        else:
            continue_sec += 1
            if continue_sec < bandage[0]:
                health += bandage[1]
                if health>maxhealth:
                    health = maxhealth

            else:
                health = health + bandage[1] + bandage[2]
                if health>maxhealth:
                    health = maxhealth
                continue_sec=0

        t+=1

    return health

공격 시간이 겹치지 않으므로 반복문이 겹치는걸 피하기 위해 딕셔너리에 담았다.

0부터 마지막 공격시간까지 1초마다 진행해가면서 분기별로 조건을 나누어서 문제를 풀었다.

profile
개발자 지망생

0개의 댓글