Algorithm

[프로그래머스] 붕대 감기 (python)

salmon16 2024. 3. 13. 21:03

출처 : https://school.programmers.co.kr/learn/courses/30/lessons/250137

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

풀이 방법

단순 구현문제이다 현재 시간에서 다음 공격 시간으로 바로 이동했다. 그런 후 공격 데이미를 입기 전에 얻는 회복량을 계산하고 데이미를 주었다. 그런 다음 현재 시간을 최근에 받은 공격 시간으로 이동했다.

 

def solution(bandage, health, attacks):        
    max_health = health
    health_time = 0
    now_t = 0
    last_time = attacks[-1][0]
    for t, d in attacks: 
        if (t - now_t > bandage[0]): ## 추가 회복량 더해주기 
            health += ((t - now_t - 1) // bandage[0]) * bandage[2]        
        health += (t - now_t -1) * bandage[1] ## 시간 만큼 회복량 더해주기
        health = min(max_health, health) ## health초과하지 않도록 설정        
        health -= d ## 데미지 만큼 빼주기
        if (health <= 0): ## 사망한 경우
            return -1
        now_t = t ## 현재 시간 초기화    
    return health

회고

문제를 풀 때 실수로 모든 기초 회복량이 1로 설정을 해서 문제를 틀렸었다.