Final Array State After K Multiplication Operations I (LeetCode 3264): Solution and Explanation

Easy
Array
Math
Heap (priority queue)
Simulation

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)

  1. Build a min-heap of (nums[i], i) for every index.
  2. Repeat k times: pop (x, i), set nums[i] = x * multiplier, and push (nums[i], i).
  3. Return nums.

Step-by-step example

nums = [2,1,3,5,6], k = 5, multiplier = 2
  1. Op 1: the minimum is 1 at index 1, so nums becomes [2,2,3,5,6].
  2. Op 2: the minimum 2 appears at indexes 0 and 1. Index 0 comes first: [4,2,3,5,6].
  3. Op 3: 2 at index 1: [4,4,3,5,6].
  4. Op 4: 3 at index 2: [4,4,6,5,6].
  5. 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 nums
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n + k log n)

Heapify is O(n) and each operation is one pop and one push.

Space

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.