Final Array State After K Multiplication Operations I (LeetCode 3264): Solution and Explanation
Apply k operations to an array. Each one finds the smallest value, taking the first one on ties, and multiplies it by multiplier. Return the final array.
Intuition
Every operation asks the same question: which position holds the smallest value, and if several do, which comes first? A linear scan answers it, but a min-heap answers it in O(log n).
The trick is what goes into the heap. Store pairs (value, index). Pairs compare by value first and by index second, so the smallest pair is exactly the first occurrence of the minimum.
After multiplying nums[i], push the updated pair (nums[i], i) back. It now competes with the other values again, and if it is still the smallest it will be picked again.
Note that the multiplied value can stay the minimum. With multiplier 1 nothing ever changes, so the same index is picked all k times.
Approach: Min-heap of (value, index)
- Build a min-heap of (nums[i], i) for every index.
- Repeat k times: pop (x, i), set nums[i] = x * multiplier, and push (nums[i], i).
- Return nums.
Step-by-step example
nums = [2,1,3,5,6], k = 5, multiplier = 2
- Op 1: the minimum is 1 at index 1, so nums becomes [2,2,3,5,6].
- Op 2: the minimum 2 appears at indexes 0 and 1. Index 0 comes first: [4,2,3,5,6].
- Op 3: 2 at index 1: [4,4,3,5,6].
- Op 4: 3 at index 2: [4,4,6,5,6].
- Op 5: 4 at indexes 0 and 1, pick index 0: [8,4,6,5,6].
Code: Python, Java, C++
1class Solution:
2 def getFinalState(self, nums: List[int], k: int, multiplier: int) -> List[int]:
3 heap = [(x, i) for i, x in enumerate(nums)]
4 heapq.heapify(heap)
5 for _ in range(k):
6 x, i = heapq.heappop(heap)
7 nums[i] = x * multiplier
8 heapq.heappush(heap, (nums[i], i))
9 return nums1class Solution {
2 public int[] getFinalState(int[] nums, int k, int multiplier) {
3 PriorityQueue<int[]> heap = new PriorityQueue<>(
4 (a, b) -> a[0] != b[0] ? Integer.compare(a[0], b[0]) : Integer.compare(a[1], b[1]));
5 for (int i = 0; i < nums.length; i++) heap.offer(new int[] {nums[i], i});
6 for (int s = 0; s < k; s++) {
7 int i = heap.poll()[1];
8 nums[i] *= multiplier;
9 heap.offer(new int[] {nums[i], i});
10 }
11 return nums;
12 }
13}1class Solution {
2public:
3 vector<int> getFinalState(vector<int>& nums, int k, int multiplier) {
4 priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> heap;
5 for (int i = 0; i < (int)nums.size(); i++) heap.push({nums[i], i});
6 for (int s = 0; s < k; s++) {
7 int i = heap.top().second;
8 heap.pop();
9 nums[i] *= multiplier;
10 heap.push({nums[i], i});
11 }
12 return nums;
13 }
14};Time and space complexity
O(n + k log n)
Heapify is O(n) and each operation is one pop and one push.
O(n)
The heap holds one pair per index.
Other approaches
Linear scan per operation (time O(n k), space O(1))
Scan for the first minimum each time. With n <= 100 and k <= 10 this is perfectly fine and simpler.
Common mistakes
- Breaking ties by the larger index. The problem wants the first occurrence, so compare indexes ascending.
- Pushing the old value back instead of the multiplied one.
- Forgetting that a multiplied element can still be the minimum and be chosen again.
Frequently asked questions
Why store the index in the heap?
To know which position to update, and to break ties toward the first occurrence.
Can values overflow?
No. The largest possible value is 100 * 5^10, about 9.8 * 10^8, which fits in a 32-bit int.
What about multiplier = 1?
Then nothing changes, and the answer is the input array.
What is the complexity?
O(n + k log n) time and O(n) space.