Arena Combo Reset Ledger

Fewest capacity-limited groups for every circular rotation

HardTwo PointersBinary LiftingGreedy

An online game's training arena stores a repeating combo as a string charges. Each character is a decimal digit giving the focus cost of one action. The order is circular: after the last action comes the first action again.

A training attempt chooses a starting index and performs exactly one full cycle of actions. The attempt must be divided into nonempty rounds. Each round contains consecutive actions from that attempt, and the sum of its action costs must be at most capacity. Focus is fully restored between rounds. No action may be split or skipped.

Return an integer array answer of length len(charges). For every index i, answer[i] is the fewest rounds needed when the attempt performs actions in this order:

i, i + 1, ..., n - 1, 0, ..., i - 1

where n = len(charges).

The capacity is guaranteed to accommodate every individual action. Zero-cost actions are allowed, including when the capacity is zero.

Examples

Example 1

Input: charges = "2413", capacity = 5
Output: [3,2,3,2]

Starting at index 0 gives costs 2, 4, 1, 3. A round containing costs 2 and 4 would exceed capacity, while costs 4 and 1 can share a round. Starting at another index changes which consecutive actions can be grouped.

Example 2

Input: charges = "000", capacity = 0
Output: [1,1,1]

All actions have zero cost, so an entire cycle fits into a single nonempty round even though the capacity is zero.

Example 3

Input: charges = "2222", capacity = 3
Output: [4,4,4,4]

Any two adjacent actions exceed the capacity. Changing the starting index cannot allow actions to share a round.

Constraints

  • 1 <= len(charges) <= 100000
  • charges contains only the characters '0' through '9'.
  • 0 <= capacity <= 9 * len(charges)
  • Every digit in charges has numeric value at most capacity.

The reference algorithm uses O(n log n) time and O(n log n) auxiliary space. Indices in the returned array refer to the original string. Keep charges as a string so that leading zero-cost actions are preserved.

Hints

Show hint 1

Duplicate the action sequence. Since costs are nonnegative, two forward-moving pointers can find the farthest exclusive endpoint of a valid round from every position.

Show hint 2

Taking the longest possible next round is optimal. Treat these endpoints as successor links, and precompute jumps of 1, 2, 4, and more rounds to answer every starting position efficiently.

Follow-up questions

What an interviewer might ask once you have a working solution.

  • For one specified starting index, return the exclusive ending positions of an optimal sequence of rounds, measured within that rotated attempt.
  • If only one starting index is queried, how can you reduce both the running time and the auxiliary space?

Practice this with an AI interviewer

Explain your approach out loud, write Python or JavaScript, run it against hidden tests (including large inputs), and get a scored debrief.

Start this problem