Take Gifts From the Richest Pile - Problem

Imagine you're a magical gift redistributor working in Santa's workshop! ๐ŸŽ

You have several piles of gifts represented by an integer array gifts, where each element shows the number of gifts in that pile. Your job is to perform a special gift reduction ritual for exactly k seconds.

Every second, you must:

  1. ๐Ÿ” Find the richest pile - Choose the pile with the maximum number of gifts
  2. โœจ Apply magic reduction - Replace the number of gifts in that pile with floor(sqrt(original_number))
  3. ๐Ÿ”„ Repeat - Continue until k seconds have passed

Goal: Return the total number of gifts remaining across all piles after k seconds of this magical process.

Example: If a pile has 25 gifts, after reduction it becomes floor(sqrt(25)) = 5 gifts.

Input & Output

example_1.py โ€” Basic Example
$ Input: gifts = [25, 64, 9, 4, 100], k = 4
โ€บ Output: 29
๐Ÿ’ก Note: Second 1: max=100 โ†’ 10, array becomes [25,64,9,4,10]. Second 2: max=64 โ†’ 8, array becomes [25,8,9,4,10]. Second 3: max=25 โ†’ 5, array becomes [5,8,9,4,10]. Second 4: max=10 โ†’ 3, array becomes [5,8,9,4,3]. Total = 5+8+9+4+3 = 29
example_2.py โ€” Small Values
$ Input: gifts = [1, 1, 1, 1], k = 4
โ€บ Output: 4
๐Ÿ’ก Note: All piles have 1 gift. floor(โˆš1) = 1, so each reduction keeps the value at 1. After 4 seconds, total remains 1+1+1+1 = 4
example_3.py โ€” Single Large Pile
$ Input: gifts = [1000000], k = 3
โ€บ Output: 15
๐Ÿ’ก Note: Second 1: 1000000 โ†’ floor(โˆš1000000) = 1000. Second 2: 1000 โ†’ floor(โˆš1000) = 31. Second 3: 31 โ†’ floor(โˆš31) = 5. Final result = 5... Wait, let me recalculate: Actually 31 โ†’ 5, but we need k=3, so it should be 1000000 โ†’ 1000 โ†’ 31 โ†’ 5. But the answer should be different. Let me recalculate properly: floor(โˆš1000000) = 1000, floor(โˆš1000) = 31, floor(โˆš31) = 5. So result is 5.

Constraints

  • 1 โ‰ค gifts.length โ‰ค 103
  • 1 โ‰ค gifts[i] โ‰ค 109
  • 1 โ‰ค k โ‰ค 103
  • All gift counts are positive integers

Visualization

Tap to expand
๐ŸŽ Magical Gift Reduction ProcessInitial State:256494100MAX!Second 1: 100 โ†’ โˆš100 = 102564MAX!9410Second 2: 64 โ†’ โˆš64 = 825MAX!89410Final Result after k=4 seconds:58943Total = 5+8+9+4+3 = 29
Understanding the Visualization
1
Initial Setup
We have piles: [25, 64, 9, 4, 100] and k=4 seconds
2
Second 1
Find max pile (100), reduce to floor(โˆš100) = 10
3
Second 2
Find max pile (64), reduce to floor(โˆš64) = 8
4
Second 3
Find max pile (25), reduce to floor(โˆš25) = 5
5
Second 4
Find max pile (10), reduce to floor(โˆš10) = 3
6
Result
Sum remaining gifts: 5+8+9+4+3 = 29
Key Takeaway
๐ŸŽฏ Key Insight: Use a max-heap to avoid scanning all piles every second - the maximum is always at the root!
Asked in
Amazon 28 Google 15 Meta 12 Microsoft 8
21.5K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen