Even Budget Split Under Per-Person Caps
Greedy & IntervalsAuto-graded · 22 test casesLast updated
Split a budget as evenly as possible among people with individual caps, using a binary-searched fill level plus a lowest-index-first remainder rule.
Even Budget Split Under Per-Person Caps
A pool holds budget units of money, to be shared among several people. Person i can receive at most caps[i] units. Every amount is a whole number.
The money is handed out one unit at a time, following this rule:
- Give the next unit to a person who currently holds the smallest amount among everyone who has not yet reached their cap.
- If several such people are tied, pick the one with the smallest index.
- Stop when the budget is used up or every person is at their cap.
Return a list with the same length as caps, where entry i is the total amount person i ends up with. If the combined caps are smaller than the budget, the surplus is simply not handed out.
Because the budget can be huge, you cannot simulate it unit by unit. You need a method whose running time does not depend on the size of budget.
Notes on the process
- Everyone starts with 0.
- A person who reaches their cap is removed from consideration and never receives more.
- Two people who are both below their cap can never differ by more than one unit once the process finishes, unless the one holding more is at their own cap.
Constraints
1 <= budget <= 10^91 <= len(caps) <= 10^51 <= caps[i] <= 10^9- All values are integers.
- The result must have
len(caps)entries, with0 <= result[i] <= caps[i].
Examples
Example 1
Input: split_budget(10, [3, 3, 3])
Output: [3, 3, 3]
Example 2
Input: split_budget(7, [2, 5, 4])
Output: [2, 3, 2]
Hints
Hint 1
Think of the process as raising a common 'level': after enough rounds, person i holds min(caps[i], level). Define total_given(level) as the sum of these values.
Hint 2
total_given(level) never decreases as level grows, so you can binary search for the largest integer level whose total does not exceed the budget.
Hint 3
After fixing that level, some units remain (fewer than the number of people still below their cap). Give one extra unit each to the lowest-index people whose cap is strictly above the level.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Picture the process as a rising water level. After enough rounds, every person who has not hit their cap holds the same amount, the level L, and everyone who has hit their cap holds exactly their cap. So the amount held at level L is min(caps[i], L), and the total paid out is
S(L) = sum(min(caps[i], L)).
S is non-decreasing in L, so we can binary search.
- Binary search over
Lin[0, max(caps)]for the largestLwithS(L) <= budget. - Give each person
min(caps[i], L). - Let
r = budget - S(L). Hand one extra unit to each of the firstrpeople (by index) whose cap is strictly greater thanL.
If budget >= sum(caps), the search ends at L = max(caps), everyone gets their full cap, and no one has a cap above L, so the surplus is discarded naturally.
Why it is correct
- The one-unit-at-a-time process always raises the current minimum among people who are not at their cap. Once all full rounds are complete, every unsaturated person sits at the same level and every saturated person sits at their cap, which is exactly
min(caps[i], L). S(L+1) - S(L)equals the number of people withcaps[i] > L. SinceS(L+1) > budget(whenL < max(caps)), the remainderris smaller than that count, so there are always enough eligible people to receive the leftover units.- During the last partial round, everyone eligible is tied at level
L, so the tie-break gives the units to the lowest indices. Nobody exceedsL + 1, and anyone who getsL + 1has a cap of at leastL + 1.
Complexity
- Time:
O(n log M)whereM = max(caps), about 30 passes over the list. - Space:
O(n)for the output,O(1)extra.
Reference solution
from typing import List
def split_budget(budget: int, caps: List[int]) -> List[int]:
# Find the largest integer level whose total payout fits in the budget.
lo, hi = 0, max(caps)
while lo < hi:
mid = (lo + hi + 1) // 2
if sum(min(c, mid) for c in caps) <= budget:
lo = mid
else:
hi = mid - 1
result = [min(c, lo) for c in caps]
remaining = budget - sum(result)
# Leftover units go to people still below their cap, lowest index first.
for i, c in enumerate(caps):
if remaining == 0:
break
if c > lo:
result[i] += 1
remaining -= 1
return result