[프로그래머스] Python - Lv.1 - [PCCP 기출문제] 1번

·2023년 12월 6일
0

코테 풀기

목록 보기
25/26
post-thumbnail

PCCP 기출문제 1번

문제/제한사항

입력/출력

문제 바로가기

PCCP 기출문제 1번


💡풀이방법

  • 입출력 예를 보면, 시간이랑 attacks의 배열 값의 첫번째 인덱스 값(공격시간)이 같은 경우 공격을 받게 된다.
  • 변수
    - current : 현재 체력
    - count : 연속체력회복
    - attack_fount : 공격여부
  1. 공격시간인 경우 (attacks 배열값의 첫번째 값)
    • 연속회복횟수 = 0
    • 현재 체력 - 공격값
    • 이때 현재 체력 < 0 or == 0인 경우: return -1
  2. 공격시간 아닌 경우
    • bandage[1]만큼 체력 회복
    • 연속회복횟수 += 1
    • 이때 연속회복횟수 == bandage[0]인 경우: 현재 체력 + bandage[2]

👀전체코드

def control_health(num, max_):
    print("control")
    if num > max_:
        print("health", max_)
        return max_
    else:
        print("current", num)
        return num

def solution(bandage, health, attacks):
    current = health # 현재 체력
    count = 0 # 연속 성공

    for i in range(1, attacks[-1][0] + 1):
        current_attack = 0
        attack_found = False 
        
        for subattack in attacks:
            if i == subattack[0]: #시간 초와 공격시간 같은 경우
                current_attack = subattack[1]
                attack_found = True # 공격 받음
                break
    
        # 공격 받은 경우
        if attack_found:
            current -= current_attack 
            count = 0
            if current < 0 or current == 0:
                return -1

        # 공격 받지 않은 경우 - 회복
        else: 
            count += 1
            # 연속 회복 성공
            if count == bandage[0]:
                current = current + bandage[1] + bandage[2]
                count = 0
            else:
                current += bandage[1]
        
        # 회복한 체력이 최대 체력을 넘지 않도록 제어
        current = control_health(current, health)
                
    return current

0개의 댓글