Count All Possible Routes Problem
Description
LeetCode Problem 1575.
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^9 + 7.
Example 1:
Input: locations = [2,3,6,8,4], start = 1, finish = 3, fuel = 5
Output: 4
Explanation: The following are all possible routes, each uses 5 units of fuel:
1 -> 3
1 -> 2 -> 3
1 -> 4 -> 3
1 -> 4 -> 2 -> 3
Example 2:
Input: locations = [4,3,1], start = 1, finish = 0, fuel = 6
Output: 5
Explanation: The following are all possible routes:
1 -> 0, used fuel = 1
1 -> 2 -> 0, used fuel = 5
1 -> 2 -> 1 -> 0, used fuel = 5
1 -> 0 -> 1 -> 0, used fuel = 3
1 -> 0 -> 1 -> 0 -> 1 -> 0, used fuel = 5
Example 3:
Input: locations = [5,2,1], start = 0, finish = 2, fuel = 3
Output: 0
Explanation: It is impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel.
Constraints:
2 <= locations.length <= 1001 <= locations[i] <= 10^9- All integers in
locationsare distinct. 0 <= start, finish < locations.length1 <= fuel <= 200
Solution
Approach: Dynamic Programming Over Remaining Fuel
A route is described by where we are and how much fuel is left; the cities visited before do not matter. Let f[k][i] be the number of routes that start at city i with k fuel and end at finish.
From city i we can stop right away if i == finish (that counts as one route), or move to any other city j that we can afford:
f[k][i] = [i == finish] + sum of f[k - cost][j] over j != i with cost = |locations[i] - locations[j]| <= k
The answer is f[fuel][start].
The locations are distinct, so every move costs at least one unit of fuel. That means f[k][i] only depends on rows with less fuel, and filling the table for k = 0, 1, ..., fuel in order always has those rows ready. Note that a route may pass through finish and come back later; the formula handles that because reaching finish adds one and the moves out of it are counted too.
Take the sum modulo 10^9 + 7. Each row adds at most 99 values below the modulus, so a 64-bit sum (or a JavaScript number) holds it exactly.
Complexity: O(n2 · fuel) time and O(n · fuel) space, where n is the number of cities.
class Solution:
def countRoutes(self, locations: List[int], start: int, finish: int, fuel: int) -> int:
mod = 10**9 + 7
n = len(locations)
# f[k][i]: routes from city i to finish with k fuel left
f = [[0] * n for _ in range(fuel + 1)]
for k in range(fuel + 1):
for i in range(n):
total = 1 if i == finish else 0 # stopping here is a route
for j in range(n):
cost = abs(locations[i] - locations[j])
if j != i and cost <= k:
total += f[k - cost][j] # every move costs > 0, so that row is done
f[k][i] = total % mod
return f[fuel][start]