Remove Stones to Minimize the Total - Problem
You are given a 0-indexed integer array piles, where piles[i] represents the number of stones in the ith pile, and an integer k. You should apply the following operation exactly k times:
Choose any piles[i] and remove floor(piles[i] / 2) stones from it.
Notice that you can apply the operation on the same pile more than once.
Return the minimum possible total number of stones remaining after applying the k operations.
floor(x) is the largest integer that is smaller than or equal to x (i.e., rounds x down).
Input & Output
Example 1 — Basic Case
$
Input:
piles = [5,4,9], k = 2
›
Output:
12
💡 Note:
Operation 1: Remove floor(9/2) = 4 stones from pile with 9 stones, leaving [5,4,5]. Operation 2: Remove floor(5/2) = 2 stones from any pile with 5 stones, leaving [5,2,5] or [3,4,5]. Total remaining = 12.
Example 2 — Single Operation
$
Input:
piles = [4,3,6,7], k = 1
›
Output:
17
💡 Note:
Remove floor(7/2) = 3 stones from the largest pile (7), leaving [4,3,6,4]. Total remaining = 4 + 3 + 6 + 4 = 17.
Example 3 — Multiple Operations on Same Pile
$
Input:
piles = [20], k = 3
›
Output:
7
💡 Note:
Operation 1: 20 → 10 (remove 10). Operation 2: 10 → 5 (remove 5). Operation 3: 5 → 3 (remove 2). Final result = 7.
Constraints
- 1 ≤ piles.length ≤ 105
- 1 ≤ piles[i] ≤ 104
- 1 ≤ k ≤ 105
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array of stone piles and k operations to perform
2
Process
Always remove floor(max_pile/2) stones from largest pile
3
Output
Sum of remaining stones after k operations
Key Takeaway
🎯 Key Insight: Always removing stones from the largest pile maximizes the total reduction
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code