leetpycode/medium/minimum_operations_to_exceed_threshold_value_ii.py

20 lines
542 B
Python

# https://leetcode.com/problems/minimum-operations-to-exceed-threshold-value-ii
from typing import List
import heapq
class Solution:
def minOperations(self, nums: List[int], k: int) -> int:
q: List[int] = []
for num in nums:
heapq.heappush(q, num)
result = 0
while len(q) > 0:
x = heapq.heappop(q)
if x >= k:
break
y = heapq.heappop(q)
heapq.heappush(q, min(x, y) * 2 + max(x, y))
result += 1
return result