Skip to content

Commit 994ac89

Browse files
committed
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 17.3 MB (30.05%)
1 parent 9144455 commit 994ac89

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<p>You are given an integer array <code>nums</code>, an integer <code>k</code>, and an integer <code>multiplier</code>.</p>
2+
3+
<p>You need to perform <code>k</code> operations on <code>nums</code>. In each operation:</p>
4+
5+
<ul>
6+
<li>Find the <strong>minimum</strong> value <code>x</code> in <code>nums</code>. If there are multiple occurrences of the minimum value, select the one that appears <strong>first</strong>.</li>
7+
<li>Replace the selected minimum value <code>x</code> with <code>x * multiplier</code>.</li>
8+
</ul>
9+
10+
<p>Return an integer array denoting the <em>final state</em> of <code>nums</code> after performing all <code>k</code> operations.</p>
11+
12+
<p>&nbsp;</p>
13+
<p><strong class="example">Example 1:</strong></p>
14+
15+
<div class="example-block">
16+
<p><strong>Input:</strong> <span class="example-io">nums = [2,1,3,5,6], k = 5, multiplier = 2</span></p>
17+
18+
<p><strong>Output:</strong> <span class="example-io">[8,4,6,5,6]</span></p>
19+
20+
<p><strong>Explanation:</strong></p>
21+
22+
<table>
23+
<tbody>
24+
<tr>
25+
<th>Operation</th>
26+
<th>Result</th>
27+
</tr>
28+
<tr>
29+
<td>After operation 1</td>
30+
<td>[2, 2, 3, 5, 6]</td>
31+
</tr>
32+
<tr>
33+
<td>After operation 2</td>
34+
<td>[4, 2, 3, 5, 6]</td>
35+
</tr>
36+
<tr>
37+
<td>After operation 3</td>
38+
<td>[4, 4, 3, 5, 6]</td>
39+
</tr>
40+
<tr>
41+
<td>After operation 4</td>
42+
<td>[4, 4, 6, 5, 6]</td>
43+
</tr>
44+
<tr>
45+
<td>After operation 5</td>
46+
<td>[8, 4, 6, 5, 6]</td>
47+
</tr>
48+
</tbody>
49+
</table>
50+
</div>
51+
52+
<p><strong class="example">Example 2:</strong></p>
53+
54+
<div class="example-block">
55+
<p><strong>Input:</strong> <span class="example-io">nums = [1,2], k = 3, multiplier = 4</span></p>
56+
57+
<p><strong>Output:</strong> <span class="example-io">[16,8]</span></p>
58+
59+
<p><strong>Explanation:</strong></p>
60+
61+
<table>
62+
<tbody>
63+
<tr>
64+
<th>Operation</th>
65+
<th>Result</th>
66+
</tr>
67+
<tr>
68+
<td>After operation 1</td>
69+
<td>[4, 2]</td>
70+
</tr>
71+
<tr>
72+
<td>After operation 2</td>
73+
<td>[4, 8]</td>
74+
</tr>
75+
<tr>
76+
<td>After operation 3</td>
77+
<td>[16, 8]</td>
78+
</tr>
79+
</tbody>
80+
</table>
81+
</div>
82+
83+
<p>&nbsp;</p>
84+
<p><strong>Constraints:</strong></p>
85+
86+
<ul>
87+
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
88+
<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
89+
<li><code>1 &lt;= k &lt;= 10</code></li>
90+
<li><code>1 &lt;= multiplier &lt;= 5</code></li>
91+
</ul>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Approach 2: Heap
2+
3+
# Time: O(n + k log n)
4+
# Space: O(n)
5+
6+
import heapq
7+
8+
class Solution:
9+
def getFinalState(self, nums: List[int], k: int, multiplier: int) -> List[int]:
10+
11+
pq = [(val, i) for i, val in enumerate(nums)]
12+
heapq.heapify(pq)
13+
14+
for _ in range(k):
15+
_, i = heapq.heappop(pq)
16+
nums[i] *= multiplier
17+
heapq.heappush(pq, (nums[i], i))
18+
19+
return nums
20+

0 commit comments

Comments
 (0)