Minimum Operations to Exceed Threshold Value II (LeetCode 3066): Solution and Explanation

Medium
Array
Heap (priority queue)
Simulation

Repeatedly replace the two smallest numbers x and y with min(x, y) * 2 + max(x, y) until every number is at least k, and count how many replacements that takes.

Intuition

The operation is fully determined: it always uses the two smallest values. There is no choice to optimize, so the answer is just the number of steps of a simulation.

The array is finished exactly when its smallest value is at least k, because then every value is. So each step needs the minimum twice and then inserts one new value. That is the job a min-heap does in O(log n).

Popping x first and y second gives x <= y automatically, so min(x, y) * 2 + max(x, y) is just x * 2 + y.

The new value can be as large as about 3 * 10^9 (x and y are both below k <= 10^9). That does not fit in a 32-bit int, so the heap must hold 64-bit values even though the answer itself is small.

Approach: Min-heap simulation

  1. Build a min-heap of nums using 64-bit values.
  2. While the heap top is less than k: pop x, pop y, push x * 2 + y, and add one to the count.
  3. Return the count.

Step-by-step example

nums = [2,11,10,1,3], k = 10
  1. Heap: [1,2,3,10,11]. The top 1 is below 10.
  2. Pop 1 and 2, push 1 * 2 + 2 = 4. Heap: [3,4,10,11], ops = 1.
  3. The top 3 is below 10. Pop 3 and 4, push 3 * 2 + 4 = 10. Heap: [10,10,11], ops = 2.
  4. The top 10 is not below 10, so stop. The answer is 2.

Code: Python, Java, C++

1class Solution:
2    def minOperations(self, nums: List[int], k: int) -> int:
3        heapq.heapify(nums)
4        ops = 0
5        while nums[0] < k:
6            x = heapq.heappop(nums)
7            y = heapq.heappop(nums)
8            heapq.heappush(nums, x * 2 + y)
9            ops += 1
10        return ops
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n log n)

Heapify is O(n), and each operation shrinks the heap by one, so there are at most n - 1 operations of O(log n) each.

Space

O(n)

The heap holds every value.

Other approaches

Re-sort after every operation (time O(n^2 log n), space O(n))

Sort, combine the first two, and sort again. Correct, but each step costs O(n log n), so the total is O(n^2 log n).

Two queues (time O(n log n), space O(n))

Sort nums into one queue and push new values into a second queue. New values come out in non-decreasing order, so the minimum is always at the front of one of the two queues. This avoids heap operations after the initial sort.

Common mistakes

  • Storing heap values as 32-bit ints. x * 2 + y can reach about 3 * 10^9 and silently overflow.
  • Checking whether every element is at least k with a full scan each step. Only the heap top matters.
  • Using a max-heap. Python's heapq is already a min-heap, which is what this problem needs.

Frequently asked questions

Why is the formula just x * 2 + y?

The heap gives the smaller value first, so x <= y, which makes min(x, y) = x and max(x, y) = y.

Can the heap run down to one element before the top reaches k?

The problem guarantees an answer exists, so that never happens on valid input.

Why is the answer an int when the values need 64 bits?

The answer counts operations, which is at most n - 1. Only the values inside the heap grow large.

What is the complexity?

O(n log n) time and O(n) space.