You are given an integer array coins (1-indexed) of length n and an integer maxJump. You can jump to any index i of the array coins if coins[i] != -1 and you have to pay coins[i] when you visit index i. In addition to that, if you are currently at index i, you can only jump to any index i + k where i + k <= n and k is a value in the range [1, maxJump].
You are initially positioned at index 1 (coins[1] is not -1). You want to find the path that reaches index n with the minimum cost.
Return an integer array of the indices that you will visit in order so that you can reach index n with the minimum cost. If there are multiple paths with the same cost, return the lexicographically smallest such path. If it is not possible to reach index n, return an empty array.
A path p1 = [Pa1, Pa2, ..., Pax] of length x is lexicographically smaller than p2 = [Pb1, Pb2, ..., Pbx] of length y, if and only if at the first j where Paj and Pbj differ, Paj < Pbj; when no such j exists, then x < y.
Example 1:
Input: coins = [1,2,4,-1,2], maxJump = 2 Output: [1,3,5]
Example 2:
Input: coins = [1,2,4,-1,2], maxJump = 1 Output: []
Constraints:
1 <= coins.length <= 1000-1 <= coins[i] <= 100coins[1] != -11 <= maxJump <= 100Problem Overview: You are given an array coins where coins[i] is the cost to land on index i. Some positions contain -1, meaning they are blocked. Starting at index 0, you can jump at most B steps forward. The goal is to reach the last index with the minimum total cost and return the path of indices taken. If multiple paths have the same cost, return the lexicographically smallest path.
Approach 1: Brute Force DFS (Exponential Time, Exponential Space)
Start from index 0 and recursively try every jump from 1 to B. Skip blocked cells (-1). Track the cumulative cost and keep the path with the smallest total cost. This explores every possible path to the end, which leads to O(B^n) time in the worst case and large recursion overhead. The approach demonstrates the search space clearly but becomes impractical for larger arrays.
Approach 2: Dynamic Programming from Right to Left (O(nB) time, O(n) space)
This is the standard solution using dynamic programming. Define dp[i] as the minimum cost needed to reach the end starting from index i. Traverse the array from right to left. For each index, check every reachable position j where i < j ≤ i + B. If coins[i] is not blocked and dp[j] is valid, update dp[i] = coins[i] + dp[j]. Store the chosen next index to reconstruct the path later.
Lexicographically smallest path naturally emerges because indices are evaluated in increasing order. After filling the DP table, reconstruct the path by following the stored next pointers starting from index 0. This approach processes each index and up to B jumps, giving O(nB) time and O(n) space.
Approach 3: Sliding Window Optimization with Deque (O(n) time, O(n) space)
The DP transition requires finding the minimum dp[j] within the window [i+1, i+B]. Instead of scanning all B positions, maintain a monotonic deque storing candidate indices with increasing dp values. As you move left across the array, remove indices that fall outside the window and keep the deque ordered by minimal cost. The front always gives the optimal next step.
This converts the transition to constant-time amortized operations and reduces the total complexity to O(n). The technique combines array traversal with a monotonic deque structure. Path reconstruction still uses a next pointer array.
Recommended for interviews: The dynamic programming from right to left solution with O(nB) time is what most interviewers expect. It clearly shows state definition, transition logic, and path reconstruction. Mentioning the deque optimization demonstrates deeper algorithmic awareness and earns extra points when discussing performance improvements.
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force DFS | O(B^n) | O(n) | Conceptual baseline to understand the search space and constraints |
| Dynamic Programming (Right to Left) | O(nB) | O(n) | General solution expected in interviews; straightforward DP with path reconstruction |
| DP with Monotonic Deque Optimization | O(n) | O(n) | Large inputs where jump window B is large and O(nB) becomes expensive |
656. Coin Path - Week 3/5 Leetcode October Challenge • Programming Live with Larry • 438 views views
Watch 1 more video solutions →Practice Coin Path with our built-in code editor and test cases.
Practice on FleetCode