Tap here to switch tabs
Problem
Submissions

Problem: Take Gifts From the Richest Pile

med
30 min
Try to solve the Take Gifts From the Richest Pile problem.

Statement

You are given an integer array gifts representing the number of gifts in various piles, and an integer k representing the number of operations to perform.

In each operation, you must:

  • Select the pile containing the maximum number of gifts. If there are multiple piles with the maximum value, you may choose any of them.

  • Replace the number of gifts in the selected pile with ⌊x⌋\lfloor \sqrt{x} \rfloor, where xx is the current number of gifts in that pile.

Return the total number of gifts remaining across all piles after performing this operation k times.

Constraints:

  • 1≤1 \leq gifts.length ≤103\leq 10^3

  • 1≤1 \leq gifts[i] ≤109\leq 10^9

  • 1≤1 \leq k ≤103\leq 10^3

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Take Gifts From the Richest Pile

med
30 min
Try to solve the Take Gifts From the Richest Pile problem.

Statement

You are given an integer array gifts representing the number of gifts in various piles, and an integer k representing the number of operations to perform.

In each operation, you must:

  • Select the pile containing the maximum number of gifts. If there are multiple piles with the maximum value, you may choose any of them.

  • Replace the number of gifts in the selected pile with ⌊x⌋\lfloor \sqrt{x} \rfloor, where xx is the current number of gifts in that pile.

Return the total number of gifts remaining across all piles after performing this operation k times.

Constraints:

  • 1≤1 \leq gifts.length ≤103\leq 10^3

  • 1≤1 \leq gifts[i] ≤109\leq 10^9

  • 1≤1 \leq k ≤103\leq 10^3