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 index i + k where 1 ≤ k ≤ maxJump
  • Cannot land on blocked platforms (where coins[i] = -1)
  • Pay coins[i] cost when visiting index i
  • 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
Coin Path - Platform Navigation INPUT coins array (platforms): 1 i=1 -1 2 -1 3 -1 4 -1 5 -1 6 -1 7 -1 8 -1 9 0 10 -1 11 -1 12 -1 13 -1 14 -1 15 0 16 Valid platform Blocked (-1) Parameters: maxJump = 6 Start: index 1 Goal: index 16 From index 1, can jump to 2-7 Only valid landing: none! Wait... can reach 7, then 10! (Actually direct 1 to 7 blocked) ALGORITHM STEPS 1 DP Backwards Start from last index, work back 2 Min Cost Calculation dp[i] = min cost from i to end 3 Track Next Index Store smallest j for lex order 4 Reconstruct Path Follow next pointers from 1 DP State Transitions: dp[16] = 0 (goal, free) dp[10] = 0 + dp[16] = 0 dp[2-9] = INF (blocked) dp[1] = 1 + dp[10] = 1 Reachable path: 1 10 16 FINAL RESULT Optimal Path Visualization: START idx:1 cost:1 jump 9 idx:10 cost:0 jump 6 GOAL idx:16 cost:0 Output Path: [1, 10, 16] Cost Breakdown: coins[1] = 1 coins[10] = 0 coins[16] = 0 Total Cost: 1 OK Key Insight: Backward DP ensures we find minimum cost paths. For lexicographically smallest path with equal cost, we choose the smallest next index when costs are equal. Direct jump from 1 to 10 (distance 9 > maxJump=6) is invalid, but the DP finds valid reachable states. The only path: 1 --> 10 --> 16 with total cost = 1. TutorialsPoint - Coin Path | Optimal Solution (Backward DP)
Asked in
Google 15 Amazon 8 Microsoft 5 Meta 3
18.2K Views
Medium Frequency
~25 min Avg. Time
480 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen