aboutsummaryrefslogtreecommitdiff
path: root/final_arr_state_k_mult1.py
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2024-12-16 01:59:39 -0700
committerJack Sangdahl <[email protected]>2024-12-16 01:59:39 -0700
commit10eb44bde3af0891caf1e5df5c6736f53b787a0a (patch)
tree1e76bcde0beec3be655ab63c308555e75c82576b /final_arr_state_k_mult1.py
parent879781c5dd20ac3fffec6567a296c6643c7403be (diff)
downloadleetcode-10eb44bde3af0891caf1e5df5c6736f53b787a0a.tar.gz
leetcode-10eb44bde3af0891caf1e5df5c6736f53b787a0a.zip
3264. final array state after k multiplication operations 1
Diffstat (limited to 'final_arr_state_k_mult1.py')
-rw-r--r--final_arr_state_k_mult1.py36
1 files changed, 36 insertions, 0 deletions
diff --git a/final_arr_state_k_mult1.py b/final_arr_state_k_mult1.py
new file mode 100644
index 0000000..caf69ac
--- /dev/null
+++ b/final_arr_state_k_mult1.py
@@ -0,0 +1,36 @@
+# 3264. Final Array State After K Multiplication Operations I
+import heapq
+
+"""
+you are given an integer array 'nums', an integer 'k', and an integer
+'multiplier'. you need to perform 'k' operations on 'nums'. in each
+operation, find the minimum value 'x' in 'nums'. if there are multiple
+occurences of the minimum value, select the one that appears first. replace
+the selected minimum value 'x' with 'x multiplier'. return an integer array
+denoting the final state of 'nums' after performing all 'k' operations
+"""
+
+
+class Solution(object):
+ def getFinalState(self, nums, k, multiplier):
+ """
+ :type nums: List[int]
+ :type k: int
+ :type multiplier: int
+ :rtype: List[int]
+ """
+ n = len(nums)
+ pq = [(nums[i], i) for i in range(n)]
+ heapq.heapify(pq)
+ while k > 0:
+ i, j = heapq.heappop(pq)
+ nums[j] = i * multiplier
+ heapq.heappush(pq, (i * multiplier, j))
+ k -= 1
+ return nums
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.getFinalState(nums=[2, 1, 3, 5, 6], k=5, multiplier=2))
+ print(obj.getFinalState(nums=[1, 2], k=3, multiplier=4))