Collect Coins with Fixed-Jump Pieces
Greedy & IntervalsAuto-graded · 23 test casesLast updated
Tests modular arithmetic insight and greedy reasoning to determine which resources are reachable under constrained movement rules.
You are given a one-dimensional board represented as an array of length n. Each cell contains one of three values:
"P"— a movable piece"C"— a collectable coin"."— an empty cell
You may perform any number of moves in any order. In each move, you select one piece currently on the board and attempt to shift it exactly 3 positions to the right (from index i to index i + 3).
A move from index i to i + 3 is valid only if all of the following conditions hold:
i + 3 < n(the destination is within bounds)- The destination cell at
i + 3does not currently contain another piece (pieces cannot overlap)
If a move is valid:
- The piece moves from index
ito indexi + 3 - If the destination cell was empty (
"."), the piece now occupies it and the cell becomes a piece - If the destination cell contained a coin (
"C"), the piece occupies it, you collect that coin, and the cell becomes a piece (the coin is removed) - The original cell at index
ibecomes empty (".") - The piece may jump over any cells between
iandi + 3; the contents of cellsi + 1andi + 2do not affect the move
Continue making moves until no valid moves remain or you choose to stop. Your goal is to collect as many coins as possible.
Return the maximum number of coins you can collect.
Constraints
1 ≤ n ≤ 10⁵- Each cell contains exactly one of
"P","C", or"." - There may be zero or more pieces and zero or more coins
Examples
Example 1
Input: collect_coins(['P', '.', '.', 'C', '.', '.', 'C'])
Output: 2
Example 2
Input: collect_coins(['C', 'P', '.', 'C', 'P', 'C', '.', 'C'])
Output: 1
Example 3
Input: collect_coins(['.', '.', 'C', '.', 'P', '.', 'C'])
Output: 0
Hints
Hint 1
A piece at index i can only move to indices i+3, i+6, i+9, etc. What property do all these indices share?
Hint 2
Consider grouping cells by their index modulo 3. Pieces in different groups never interact.
Hint 3
Within a single modulo-3 group, which coins can actually be reached by the pieces in that group? Think about the leftmost piece.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The key insight is that pieces move in increments of 3, so a piece at index i can only ever reach indices i, i+3, i+6, i+9, and so on. This means each piece remains in the same residue class modulo 3 throughout all moves.
We can partition the board into three independent lanes based on index modulo 3:
- Lane 0: indices 0, 3, 6, 9, ...
- Lane 1: indices 1, 4, 7, 10, ...
- Lane 2: indices 2, 5, 8, 11, ...
Pieces and coins in different lanes never interact. Within each lane, a piece effectively steps forward one position at a time along that lane's sequence.
Critical observation: Within a single lane, a coin is collectible if and only if there exists at least one piece in that lane positioned to the left of (at a smaller index than) the coin.
Why? Consider a lane with pieces at positions p₁ < p₂ < ... < pₖ. If we move pieces in right-to-left order (starting with the rightmost piece), each piece can travel as far right as possible without being blocked by pieces that haven't moved yet. The rightmost piece moves to the end of the lane, the second-rightmost moves to one position before that, and so on. This strategy ensures that every piece sweeps through all cells between its starting position and its final position, collecting every coin along the way.
Any coin positioned after the leftmost piece in its lane will eventually be passed by at least one piece using this strategy. Conversely, coins positioned before all pieces in their lane are unreachable.
Algorithm:
- For each of the three lanes (residue classes 0, 1, 2), find the index of the first (leftmost) piece
- Count all coins that appear in a lane after that lane's first piece
This avoids any simulation and runs in linear time.
Complexity
- Time: O(n) — two linear scans through the board
- Space: O(1) — only three integers to track the first piece in each lane
Solution
from typing import List
def collect_coins(board: List[str]) -> int:
first_piece = [None, None, None]
for i, cell in enumerate(board):
if cell == "P" and first_piece[i % 3] is None:
first_piece[i % 3] = i
return sum(
1 for i, cell in enumerate(board)
if cell == "C" and first_piece[i % 3] is not None and i > first_piece[i % 3]
)