Emptying Depots With a Limited Pool of Partner Skips
Greedy & IntervalsAuto-graded · 22 test casesLast updated
Compute the minimum skips each depot needs using modular arithmetic, then greedily pick the cheapest depots under a shared skip budget to maximize emptied depots.
Emptying Depots With a Limited Pool of Partner Skips
You and a partner are clearing a set of depots, one depot at a time. Depot i starts with stock[i] units.
On each depot, the two of you alternate turns, and you always move first:
- On your turn you remove
my_amountunits. - On your partner's turn they remove
partner_amountunits.
A depot is finished once its stock is 0 or less. You earn one point for a depot only if your removal is the one that brings its stock to 0 or below. If your partner's removal empties it, you earn nothing for that depot.
Your partner may skip some of their turns. When they skip, you immediately take the next turn as well, so you remove my_amount again. Across all depots combined, the partner may skip at most skips times. You decide where the skips are used, and you may use fewer than skips.
Return the maximum number of points you can earn over all depots.
Details
- Every depot begins with your turn, regardless of how earlier depots went.
- Depots are independent apart from sharing the single skip budget.
- A skip only matters when it replaces a partner turn. Skips on a depot where you do not earn a point are wasteful, so only spend skips where they lead to a point.
Function
Implement max_credits_with_skips(stock, my_amount, partner_amount, skips) returning an integer.
Constraints
1 <= len(stock) <= 10^51 <= stock[i], my_amount, partner_amount, skips <= 10^9- Simulating turn by turn is too slow for the given limits.
Examples
Example 1
Input: max_credits_with_skips([3, 6, 9], 2, 3, 2)
Output: 3
Example 2
Input: max_credits_with_skips([10, 10, 10], 3, 2, 1)
Output: 1
Hints
Hint 1
Treat each depot on its own first: ask what is the smallest number of skips that guarantees you finish it. Then think about how to combine those numbers across depots.
Hint 2
Every full round (you, then partner) removes my_amount + partner_amount units and returns to your turn, so only the remainder of stock modulo that sum matters. If the remainder is 0, treat it as a full round, not as zero.
Hint 3
With remainder r left on your turn, you need ceil(r / my_amount) of your own removals in a row, and the first is free. So the cost is ceil(r / my_amount) - 1. Every point is worth the same, so sort the costs and take the cheapest ones while the budget lasts.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The depots only interact through the shared skip budget, so split the problem in two:
- For each depot, compute the minimum number of skips needed for you to be the one who empties it.
- Every successful depot is worth exactly one point, so choose the cheapest depots first until the budget runs out.
Cost of a single depot
One full round (you, then your partner) removes cycle = my_amount + partner_amount units and hands the turn back to you. Full rounds do not change who finishes the depot, so only the remainder matters:
r = stock % cycle
if r == 0: r = cycle # the last stretch is a full round, not an empty depot
Now 0 < r <= cycle and it is your turn.
- If
r <= my_amount, your next removal empties the depot, so the cost is 0. - Otherwise your partner would finish it unless they skip. You need to cover all of
ralone, which takesceil(r / my_amount)removals in a row. The first is your natural turn and each of the others needs one skip.
So cost = ceil(r / my_amount) - 1, computed as (r + my_amount - 1) // my_amount - 1. This formula also yields 0 when r <= my_amount.
Greedy selection
Sort the costs ascending and take depots while the running total stays within skips. Stop at the first depot that cannot be afforded, since all later ones cost at least as much.
Why it is correct
- Reducing modulo
cycleis safe: the end of the depot decides who finishes, and the skips are needed only in that final stretch. The minimum cost is determined by the remainderrwhen it is your turn. - If your partner removes anything in the final
runits, they finish it whenever their amount reaches the remaining stock. Avoiding that requires you to coverralone, soceil(r / my_amount)removals is the minimum, which givesceil(r / my_amount) - 1skips. - Since every point is worth 1, an exchange argument shows the
kcheapest depots are affordable whenever anykdepots are: swapping any chosen depot for a cheaper unchosen one never raises the total cost.
Complexity
- Time: O(n log n), dominated by sorting the costs.
- Space: O(n) for the cost list.
Reference solution
from typing import List
def max_credits_with_skips(stock: List[int], my_amount: int, partner_amount: int, skips: int) -> int:
cycle = my_amount + partner_amount
costs = []
for units in stock:
r = units % cycle
if r == 0:
r = cycle
# ceil(r / my_amount) consecutive removals by me, the first one is free
cost = (r + my_amount - 1) // my_amount - 1
costs.append(cost)
costs.sort()
points = 0
remaining = skips
for cost in costs:
if cost > remaining:
break
remaining -= cost
points += 1
return points