Count All Possible Routes - Problem

You are given an array of distinct positive integers locations where locations[i] represents the position of city i. You are also given integers start, finish and fuel representing the starting city, ending city, and the initial amount of fuel you have, respectively.

At each step, if you are at city i, you can pick any city j such that j != i and 0 <= j < locations.length and move to city j. Moving from city i to city j reduces the amount of fuel you have by |locations[i] - locations[j]|. Please notice that |x| denotes the absolute value of x.

Notice that fuel cannot become negative at any point in time, and that you are allowed to visit any city more than once (including start and finish).

Return the count of all possible routes from start to finish. Since the answer may be too large, return it modulo 10⁹ + 7.

Input & Output

Example 1 — Basic Case
$ Input: locations = [2,3,6,8,4], start = 1, finish = 3, fuel = 5
Output: 4
💡 Note: Starting at city 1 (position 3) with 5 fuel, there are 4 possible routes to reach city 3 (position 8): 1→3 (cost 5), 1→0→3 (cost 1+6=7 > 5, invalid), 1→2→3 (cost 3+2=5), 1→4→3 (cost 1+4=5), and 1→4→0→3 (partial route within fuel limit).
Example 2 — Direct Route Only
$ Input: locations = [4,3,1], start = 1, finish = 0, fuel = 6
Output: 5
💡 Note: From city 1 (position 3) to city 0 (position 4) with 6 fuel, multiple paths exist including detours through city 2.
Example 3 — Insufficient Fuel
$ Input: locations = [5,2,1], start = 0, finish = 2, fuel = 3
Output: 0
💡 Note: From city 0 (position 5) to city 2 (position 1) requires 4 fuel minimum, but we only have 3.

Constraints

  • 2 ≤ locations.length ≤ 100
  • 1 ≤ locations[i] ≤ 109
  • All integers in locations are distinct
  • 0 ≤ start, finish < locations.length
  • 1 ≤ fuel ≤ 200

Visualization

Tap to expand
Count All Possible Routes INPUT Cities on Number Line 0 pos:2 1 START pos:3 4 pos:4 2 pos:6 3 FINISH pos:8 locations = [2,3,6,8,4] start = 1 finish = 3 fuel = 5 Fuel Cost = |pos[i] - pos[j]| City 1 --> City 3: |3-8| = 5 City 1 --> City 4: |3-4| = 1 City 4 --> City 3: |4-8| = 4 City 1 --> City 2: |3-6| = 3 ALGORITHM STEPS 1 Define DP State dp[city][fuel] = routes to reach finish 2 Base Case At finish city: +1 route dp[3][f] starts with 1 3 Recurrence For each reachable city j: dp[i][f] += dp[j][f-cost] 4 Memoization Cache results, mod 10^9+7 Sample DP Values State Routes dp[1][5] 4 (answer) dp[3][0] 1 (at finish) dp[4][4] 1 (via city 4) FINAL RESULT 4 Valid Routes Found Route 1: 1 --> 3 (fuel: 5-5=0) Route 2: 1 --> 4 --> 3 (1+4=5) Route 3: 1 --> 2 --> 3 (3+2=5) Route 4: 1 --> 0 --> 4 --> 3 (1+2+4=7) Wait - this exceeds fuel! Route 4 (correct): 1 --> 4 --> 2 --> 3 (1+2+2=5) OUTPUT 4 Key Insight: Use memoization with state (current_city, remaining_fuel). From each city, try all other cities if fuel allows. Sum up routes recursively. Cities can be revisited, making DP essential for efficiency. TutorialsPoint - Count All Possible Routes | Dynamic Programming Approach
Asked in
Google 15 Amazon 12 Microsoft 8 Apple 5
38.5K Views
Medium Frequency
~25 min Avg. Time
892 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