less than 1 minute read

1. 개요

2. 풀이

from heapq import heappushpop, heappush

def solution(n, k, enemies):
    # 현재 처리한 적의 수
    killedEnemies = []
    
    # 현재 내가 소모한 병사의 수
    usedSoldiers = 0
    
    # 현재 라운드
    curRound = 0

    for enemy in enemies:
        # 매라운드의 적과 싸우면서 내가 소모한 병사의 수가 많아진다.
        usedSoldiers += enemy
        
        # 내가 소모한 병사의수가 n보다 작으면 killedEnemies에 현재 라운드의 적을 추가
        # 다음 라운드로 진행
        if usedSoldiers <= n:
            heappush(killedEnemies, -enemy)
            curRound += 1

        # 못진행하더라고 괜찮다! 우리에게는 k가 있다면 문제가 없다.
        # 매 라운드에 처리한 적들중 가장 많은 적을 포함한 라운드를 힙을 통해 빼내서
        # k로 퉁치고, 아군의 수를 늘릴수 있다.
        elif k > 0:
            k -= 1
            usedSoldiers += heappushpop(killedEnemies, -enemy)
            curRound += 1
            
        # 여기까지 왔다면 가망이 없다.
        else:
            break

    return curRound

Leave a comment