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:
x in nums. If there are multiple occurrences of the minimum value, select the one that appears first.x with x * multiplier.Return an integer array denoting the final state of nums after performing all k operations.
Example 1:
Input: nums = [2,1,3,5,6], k = 5, multiplier = 2
Output: [8,4,6,5,6]
Explanation:
| Operation | Result |
|---|---|
| After operation 1 | [2, 2, 3, 5, 6] |
| After operation 2 | [4, 2, 3, 5, 6] |
| After operation 3 | [4, 4, 3, 5, 6] |
| After operation 4 | [4, 4, 6, 5, 6] |
| After operation 5 | [8, 4, 6, 5, 6] |
Example 2:
Input: nums = [1,2], k = 3, multiplier = 4
Output: [16,8]
Explanation:
| Operation | Result |
|---|---|
| After operation 1 | [4, 2] |
| After operation 2 | [4, 8] |
| After operation 3 | [16, 8] |
Constraints:
1 <= nums.length <= 1001 <= nums[i] <= 1001 <= k <= 101 <= multiplier <= 5Problem Overview: You receive an integer array nums, an integer k, and a multiplier. Perform exactly k operations. In each operation, find the smallest value in the array (choose the smallest index if there is a tie), multiply it by multiplier, and update the array. After all operations, return the final state of the array.
This is mainly a simulation problem built around repeatedly selecting the minimum element. The main design decision is how efficiently you retrieve that minimum value during each operation.
Approach 1: Simple Iteration Approach (O(n * k) time, O(1) space)
The most straightforward solution simulates the process exactly as described. For each of the k operations, iterate through the entire array to find the smallest value and its index. Once found, multiply that element by multiplier and continue to the next operation. The algorithm uses a simple linear scan of the array each time the minimum must be identified.
This approach works well when k and n are small. The implementation is extremely simple and mirrors the problem statement directly. However, every operation requires scanning the entire array, resulting in O(n) work per operation and O(n * k) total time.
Approach 2: Using Min-Heap (Priority Queue) (O((n + k) log n) time, O(n) space)
A more efficient solution maintains the current minimum using a min-heap (priority queue). Push all elements into the heap as pairs (value, index). The heap automatically keeps the smallest value at the top, and the index ensures ties are resolved correctly.
During each operation, pop the smallest element from the heap, multiply its value by multiplier, update the corresponding index in the array, and push the updated pair back into the heap. Each pop and push operation costs O(log n), so performing k operations results in O(k log n) heap work after the initial O(n) heap construction.
This method avoids repeated full scans of the array and is ideal when k is large. It models the process as a simulation but relies on a heap to keep minimum lookups efficient.
Recommended for interviews: Start by explaining the direct simulation using iteration to demonstrate understanding of the rules. Then move to the min‑heap optimization. Interviewers typically expect the heap solution because it reduces repeated minimum searches and scales better when the number of operations grows.
This approach involves simply iterating through the array to find the minimum element and multiplying it by the multiplier. This process is repeated for k times. It's straightforward due to the relatively small constraints (nums.length <= 100).
The function finalArrayState takes an array, its size, the number of operations (k), and a multiplier. It iteratively finds the minimum value in the array and multiplies it by the multiplier. This process is repeated k times, updating the array in-place.
Time Complexity: O(k * n), where n is the number of elements in the array, because for each of k iterations, we search through the array to find the minimum.
Space Complexity: O(1), as we do not use extra space apart from basic variables.
In this approach, a min-heap (priority queue) is used to efficiently retrieve the minimum element. The heap is updated on every multiplication operation, ensuring that the minimum value is always at the root.
The function uses a min-heap, built with tuples containing the element and its index. This allows the heap to manage duplicate values correctly and to quickly access and update the minimum value. It ensures the constraint of selecting the first occurrence of a minimum value by utilizing the index in conjunction with the heap operations.
Time Complexity: O(n + k log n), where n is the size of the array for the heapify operation, and k log n for the subsequent heap operations on each element.
Space Complexity: O(n) because a heap stores all elements and additional structure is used for managing index tracking.
We can use a min-heap to maintain the elements in the array nums. Each time, we extract the minimum value from the min-heap, multiply it by multiplier, and then put it back into the min-heap. During the implementation, we insert the indices of the elements into the min-heap and define a custom comparator function to sort the min-heap based on the values of the elements in nums as the primary key and the indices as the secondary key.
Finally, we return the array nums.
The time complexity is O((n + k) times log n), and the space complexity is O(n). Here, n is the length of the array nums.
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Simple Iteration Approach | Time Complexity: O(k * n), where n is the number of elements in the array, because for each of k iterations, we search through the array to find the minimum. |
| Using Min-Heap (Priority Queue) | Time Complexity: O(n + k log n), where n is the size of the array for the heapify operation, and k log n for the subsequent heap operations on each element. |
| Priority Queue (Min-Heap) + Simulation | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Simple Iteration (Linear Scan) | O(n * k) | O(1) | Small arrays or small k where simplicity matters more than optimization |
| Min-Heap (Priority Queue) | O((n + k) log n) | O(n) | Large k or repeated minimum lookups where heap structure avoids full scans |
Final Array State After K Multiplication Operations I | Detailed | Leetcode 3264 | codestorywithMIK • codestorywithMIK • 4,306 views views
Watch 9 more video solutions →Practice Final Array State After K Multiplication Operations I with our built-in code editor and test cases.
Practice on FleetCode