Coin Path - Problem
Imagine navigating through a platformer game where you start at the first platform and need to reach the final platform with the minimum cost. You have a limited jump range and must pay a toll to land on each platform!
Given a 1-indexed array coins where coins[i] represents the cost to land on platform i (or -1 if blocked), and an integer maxJump representing your maximum jump distance, find the lexicographically smallest path that reaches the final platform with minimum cost.
Rules:
- Start at index 1 (guaranteed to be accessible)
- From index
i, you can jump to any indexi + kwhere1 ≤ k ≤ maxJump - Cannot land on blocked platforms (where
coins[i] = -1) - Pay
coins[i]cost when visiting indexi - If multiple paths have the same minimum cost, return the lexicographically smallest one
Example: coins = [1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1, -1, -1, 0], maxJump = 6
You can jump from index 1 → 10 → 16 with total cost 1+0+0 = 1
Input & Output
example_1.py — Basic Path Finding
$
Input:
coins = [1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1, -1, -1, 0], maxJump = 6
›
Output:
[1, 10, 16]
💡 Note:
Start at index 1 (cost 1), jump to index 10 (cost 0), then jump to index 16 (cost 0). Total cost = 1 + 0 + 0 = 1. This is the minimum cost path.
example_2.py — Lexicographically Smallest Path
$
Input:
coins = [1, 2, 4, -1, 2], maxJump = 2
›
Output:
[1, 2, 5]
💡 Note:
Two paths exist: [1,3,5] with cost 1+4+2=7 and [1,2,5] with cost 1+2+2=5. The second path [1,2,5] has lower cost, so it's the answer.
example_3.py — Impossible Path
$
Input:
coins = [1, 2, -1], maxJump = 1
›
Output:
[]
💡 Note:
Cannot reach the end because index 3 (coins[2]) is blocked with value -1, and maxJump=1 means we can only jump 1 position at a time.
Constraints
- 1 ≤ coins.length ≤ 1000
- 0 ≤ coins[i] ≤ 109 or coins[i] = -1
- 1 ≤ maxJump ≤ 1000
- coins[0] ≠ -1 (starting position is always accessible)
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code