KatInterview

Emptying Depots With a Limited Pool of Partner Skips

Greedy & IntervalsAuto-graded · 22 test cases
MediumAuto-gradedGreedy & Intervalsgreedymathsorting

Last 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_amount units.
  • On your partner's turn they remove partner_amount units.

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^5
  • 1 <= 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.