Skip to content

Commit accf63e

Browse files
committed
Sync LeetCode submission Runtime - 255 ms (46.22%), Memory - 35.4 MB (91.60%)
1 parent 7fd8e32 commit accf63e

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and an integer <code>k</code>.</p>
2+
3+
<p>In one operation, you will:</p>
4+
5+
<ul>
6+
<li>Take the two smallest integers <code>x</code> and <code>y</code> in <code>nums</code>.</li>
7+
<li>Remove <code>x</code> and <code>y</code> from <code>nums</code>.</li>
8+
<li>Add <code>min(x, y) * 2 + max(x, y)</code> anywhere in the array.</li>
9+
</ul>
10+
11+
<p><strong>Note</strong> that you can only apply the described operation if <code>nums</code> contains at least two elements.</p>
12+
13+
<p>Return <em>the <strong>minimum</strong> number of operations needed so that all elements of the array are greater than or equal to</em> <code>k</code>.</p>
14+
15+
<p>&nbsp;</p>
16+
<p><strong class="example">Example 1:</strong></p>
17+
18+
<pre>
19+
<strong>Input:</strong> nums = [2,11,10,1,3], k = 10
20+
<strong>Output:</strong> 2
21+
<strong>Explanation:</strong> In the first operation, we remove elements 1 and 2, then add 1 * 2 + 2 to nums. nums becomes equal to [4, 11, 10, 3].
22+
In the second operation, we remove elements 3 and 4, then add 3 * 2 + 4 to nums. nums becomes equal to [10, 11, 10].
23+
At this stage, all the elements of nums are greater than or equal to 10 so we can stop.
24+
It can be shown that 2 is the minimum number of operations needed so that all elements of the array are greater than or equal to 10.
25+
</pre>
26+
27+
<p><strong class="example">Example 2:</strong></p>
28+
29+
<pre>
30+
<strong>Input:</strong> nums = [1,1,2,4,9], k = 20
31+
<strong>Output:</strong> 4
32+
<strong>Explanation:</strong> After one operation, nums becomes equal to [2, 4, 9, 3].
33+
After two operations, nums becomes equal to [7, 4, 9].
34+
After three operations, nums becomes equal to [15, 9].
35+
After four operations, nums becomes equal to [33].
36+
At this stage, all the elements of nums are greater than 20 so we can stop.
37+
It can be shown that 4 is the minimum number of operations needed so that all elements of the array are greater than or equal to 20.</pre>
38+
39+
<p>&nbsp;</p>
40+
<p><strong>Constraints:</strong></p>
41+
42+
<ul>
43+
<li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li>
44+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
45+
<li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li>
46+
<li>The input is generated such that an answer always exists. That is, there exists some sequence of operations after which all elements of the array are greater than or equal to <code>k</code>.</li>
47+
</ul>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Approach: Priority Queue
2+
3+
# Time: O(n log n)
4+
# Space: O(n)
5+
6+
import heapq
7+
8+
class Solution:
9+
def minOperations(self, nums: List[int], k: int) -> int:
10+
heapq.heapify(nums)
11+
12+
num_oper = 0
13+
14+
while nums[0] < k:
15+
x = heapq.heappop(nums)
16+
y = heapq.heappop(nums)
17+
heapq.heappush(nums, min(x, y) * 2 + max(x, y))
18+
num_oper += 1
19+
20+
return num_oper

0 commit comments

Comments
 (0)