Take Gifts From the Richest Pile (LeetCode 2558): Solution and Explanation

Easy
Array
Heap (priority queue)
Simulation

Every second the largest pile of gifts is cut down to the floor of its square root. After k seconds, return how many gifts are left across all piles.

Intuition

The rule always touches the single largest pile, and after it shrinks some other pile may become the largest. So the task is: find the maximum, replace it with a smaller value, and repeat.

Scanning all piles each second works, but a max-heap does the same job in O(log n) per second. The top of the heap is the richest pile; pop it, compute floor(sqrt(x)), and push the result back.

Square roots shrink big numbers very fast: one billion becomes 31,622 in one step and 177 in the next. That is why a few seconds can remove almost all the gifts from a huge pile.

After k seconds the answer is just the sum of what is left in the heap. With up to 1,000 piles of up to 10^9 gifts the sum can exceed 32 bits, so accumulate it in a 64-bit integer.

Approach: Max-heap simulation

  1. Push every pile into a max-heap.
  2. Repeat k times: pop the largest pile x and push floor(sqrt(x)).
  3. Sum the values left in the heap with a 64-bit accumulator and return it.

Step-by-step example

gifts = [25,64,9,4,100], k = 4
  1. Second 1: the richest pile is 100. Leave 10, take 90.
  2. Second 2: the richest is 64. Leave 8.
  3. Second 3: the richest is 25. Leave 5.
  4. Second 4: the richest is now 10. Leave 3.
  5. Piles [5,8,9,4,3] sum to 29.

Code: Python, Java, C++

1class Solution:
2    def pickGifts(self, gifts: List[int], k: int) -> int:
3        heap = [-g for g in gifts]
4        heapq.heapify(heap)
5        for _ in range(k):
6            richest = -heapq.heappop(heap)
7            heapq.heappush(heap, -math.isqrt(richest))
8        return -sum(heap)
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 of the k seconds does one pop and one push.

Space

O(n)

The heap stores every pile.

Other approaches

Scan for the maximum each second (time O(n k), space O(1))

Find the largest pile with a linear scan, shrink it in place, and repeat. With n and k at most 1,000 this is fast enough, but it does not scale.

Stop early (time O(n + k log n), space O(n))

Once the top of the heap is 1, every pile is 1 and nothing else changes, so you can break out of the loop.

Common mistakes

  • Summing into a 32-bit int. Ten piles of 10^9 already overflow.
  • Using a min-heap by mistake. Python's heapq is a min-heap, so store negated values.
  • Floating point square roots on huge values. Use math.isqrt in Python; for values up to 10^9, a double sqrt followed by a floor is also exact.

Frequently asked questions

Does it matter which pile is chosen on a tie?

No. Tied piles hold the same number of gifts, so shrinking either one leaves the same multiset of piles and the same final sum.

Why a heap and not sorting once?

After a pile shrinks it moves to a new place in the order. A heap keeps the order up to date in O(log n) per change.

Can the loop end early?

Yes. If the largest pile is 1, floor(sqrt(1)) = 1, so nothing changes any more.

What is the complexity?

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