Skip to main content
LeetCode 2558, Easy. Topics: Array, Heap (Priority Queue), Simulation. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 18 parametrized pytest cases, and a playground notebook:

Problem

You are given an integer array gifts denoting the number of gifts in various piles. Every second, you do the following:
  • Choose the pile with the maximum number of gifts.
  • If there is more than one pile with the maximum number of gifts, choose any.
  • Reduce the number of gifts in the pile to the floor of the square root of the original number of gifts in the pile.
Return the number of gifts remaining after k seconds.

Examples

Constraints

  • 1 <= gifts.length <= 10<sup>3</sup>
  • 1 <= gifts[i] <= 10<sup>9</sup>
  • 1 <= k <= 10<sup>3</sup>

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

NeetCode All.
Last modified on September 7, 2026