Skip to content

Commit 10db8ef

Browse files
authored
Create minimum-operations-to-exceed-threshold-value-ii.py
1 parent 9034887 commit 10db8ef

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Time: O(nlogn)
2+
# Space: O(n)
3+
4+
import heapq
5+
6+
7+
# simulation, heap
8+
class Solution(object):
9+
def minOperations(self, nums, k):
10+
"""
11+
:type nums: List[int]
12+
:type k: int
13+
:rtype: int
14+
"""
15+
result = 0
16+
heapq.heapify(nums)
17+
while nums:
18+
if nums[0] >= k:
19+
break
20+
mn1 = heapq.heappop(nums)
21+
mn2 = heapq.heappop(nums)
22+
heapq.heappush(nums, 2*mn1+mn2)
23+
result += 1
24+
return result

0 commit comments

Comments
 (0)