description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: n = len(locations) f = [[(0) for j in range(n)] for i in range(fuel + 1)] LARGE = int(1000000000.0 + 7) f[fuel][start] = 1 for v in range(fuel, -1, -1): for i, loc1 in enumerate(locations): if f[v][i] > 0: for j, loc2 in enumerate(locations): if j != i: update_fuel = v - abs(loc1 - loc2) if update_fuel >= 0: f[update_fuel][j] += f[v][i] % LARGE result = sum([f[i][finish] for i in range(fuel)]) if start == finish: result += 1 return result % LARGE
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER RETURN BIN_OP VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: def cnt(locations, cur, fuel, memo): if (cur, fuel) in memo: return memo[cur, fuel] n = len(locations) result = 0 if cur == finish: result = 1 for i in range(n): if i != cur: cost = abs(locations[i] - locations[cur]) if cost <= fuel: result += cnt(locations, i, fuel - cost, memo) memo[cur, fuel] = result return result def dp(locations, start, finish, fuel): n = len(locations) tbl = [[(0) for j in range(fuel + 1)] for i in range(n)] tbl[start][0] = 1 for f in range(1, fuel + 1): tbl[start][f] = 1 for cur in range(n): for last in range(n): if cur != last: cost = abs(locations[cur] - locations[last]) if f >= cost: tbl[cur][f] += tbl[last][f - cost] return tbl[finish][fuel] return dp(locations, start, finish, fuel) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: cache = {} def dp(i, fuel): if (i, fuel) in cache: return cache[i, fuel] count = 1 if i == finish else 0 for j in range(len(locations)): if i != j: dist = abs(locations[i] - locations[j]) if dist <= fuel: count += dp(j, fuel - dist) cache[i, fuel] = count return count return dp(start, fuel) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: if abs(locations[finish] - locations[start]) > fuel: return 0 M = 1000000007 n = len(locations) dp = [([0] * n) for i in range(fuel + 1)] for i in range(fuel + 1): dp[i][finish] = 1 for f in range(1, fuel + 1): for s in range(n): for s1 in range(n): if s == s1: continue if abs(locations[s1] - locations[s]) <= f: dp[f][s] = ( dp[f][s] + dp[f - abs(locations[s] - locations[s1])][s1] ) % M return dp[fuel][start]
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: n = len(locations) dp = [([-1] * (fuel + 1)) for _ in range(n)] def solve(current, fuel): if fuel < 0: return 0 if dp[current][fuel] != -1: return dp[current][fuel] ans = 1 if current == finish else 0 for next in range(n): if next != current: ans = ( ans + solve(next, fuel - abs(locations[current] - locations[next])) ) % 1000000007 dp[current][fuel] = ans return ans return solve(start, fuel)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: def go(pos, f, dp): if f == 0: return 1 if pos == finish else 0 if dp[pos][f] > -1: return dp[pos][f] ans = 1 if pos == finish else 0 for idx, loc in enumerate(locations): if idx != pos: c = abs(locations[pos] - loc) ans += go(idx, f - c, dp) if f >= c else 0 dp[pos][f] = ans return ans memo = [([-1] * (fuel + 1)) for _ in locations] memo[finish][0] = 1 return go(start, fuel, memo) % 1000000007
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: start_val, finish_val = locations[start], locations[finish] locations.sort() start, finish = bisect.bisect_left(locations, start_val), bisect.bisect_left( locations, finish_val ) res = 0 n = len(locations) dp = [([0] * n) for i in range(fuel + 1)] dp[0][start] = 1 for i in range(0, fuel + 1): for j in range(n): p = locations[j] indl, indr = bisect.bisect_left( locations, p - (fuel - i) ), bisect.bisect_right(locations, p + (fuel - i)) for k in range(indl, indr): if k == j: continue dis = abs(locations[k] - p) dp[dis + i][k] += dp[i][j] for i in range(fuel + 1): res += dp[i][finish] return res % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: @lru_cache(None) def dfs(i: int, f: int) -> int: if f < 0: return 0 return (1 if i == finish else 0) + sum( 0 if i == j else dfs(j, f - abs(locations[j] - locations[i])) for j in range(len(locations)) ) return dfs(start, fuel) % 1000000007
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR FUNC_DEF VAR VAR IF VAR NUMBER RETURN NUMBER RETURN BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR NONE VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: mod = 10**9 + 7 n = len(locations) dp = [([0] * n) for _ in range(201)] for f in range(201): dp[f][finish] = 1 for ff in range(1, fuel + 1): for node in range(n): res = 0 for i, j in enumerate(locations): if i == node: continue need = abs(j - locations[node]) if need <= ff: res = (res + dp[ff - need][i]) % mod dp[ff][node] = (res + dp[ff][node]) % mod return dp[fuel][start]
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: @lru_cache(None) def dfs(x, y, f): if abs(A[x] - A[y]) > f: return 0 ret = 0 if abs(A[x] - A[y]) <= f: ret += 1 for mid in range(n): g = abs(A[mid] - A[x]) + abs(A[mid] - A[y]) if mid == x or g > f: continue ret += dfs(mid, y, f - abs(A[mid] - A[x])) if mid == y: ret -= 1 ret %= MOD return ret A = locations n = len(A) MOD = 10**9 + 7 return dfs(start, finish, fuel)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR FUNC_DEF IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR RETURN VAR FUNC_CALL VAR NONE ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: memo = {} n = len(locations) def dfs(cur, i): if cur < 0: return 0 if (cur, i) not in memo: ans = 0 if i == finish and cur >= 0: ans += 1 for j in range(n): if j == i: continue ans += dfs(cur - abs(locations[i] - locations[j]), j) ans %= 10**9 + 7 memo[cur, i] = ans return memo[cur, i] return dfs(fuel, start)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: mod = 10**9 + 7 def dfs(i, f, dp): key = i, f if key in dp: return dp[key] ans = 1 if i == finish else 0 li = locations[i] for j, l in enumerate(locations): if j == i: continue cost = abs(l - li) if cost > f: continue ans = (ans + dfs(j, f - cost, dp)) % mod dp[key] = ans return ans return dfs(start, fuel, {})
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR VAR IF VAR VAR RETURN VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR DICT VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: @lru_cache(None) def dfs(current, fuel): ans = 0 if fuel >= 0: if current == finish: ans += 1 for i, loc in enumerate(locations): if i != current: consume = abs(locations[current] - loc) ans += dfs(i, fuel - consume) return ans % (10**9 + 7) ret = dfs(start, fuel) return ret
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_CALL VAR NONE ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: @lru_cache(None) def dfs(start, finish, fuel): if abs(locations[start] - locations[finish]) > fuel: return 0 res = 1 if start == finish else 0 for i, v in enumerate(locations): if i == start: continue res += dfs(i, finish, fuel - abs(v - locations[start])) return res mod = 10**9 + 7 return dfs(start, finish, fuel) % mod
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR FUNC_DEF IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN VAR FUNC_CALL VAR NONE ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
MOD = 10**9 + 7 def gen_matrix(m, n, elem): return [[elem for col in range(n)] for row in range(m)] def count_routes(cities, start, finish, fuel): m_routes = gen_matrix(fuel + 1, len(cities), None) def dist(c1, c2): return abs(cities[c1] - cities[c2]) def routes(f, c): if f < 0: return 0 if m_routes[f][c] is not None: return m_routes[f][c] if f == 0 and c != finish: ans = 0 else: current_sol = bool(c == finish) ans = current_sol + sum( routes(f - dist(c, c2), c2) for c2 in range(len(cities)) if c2 != c ) ans %= MOD m_routes[f][c] = ans return ans return routes(fuel, start) class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: return count_routes(locations, start, finish, fuel)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF RETURN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR NONE FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR NONE RETURN VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: if abs(locations[start] - locations[finish]) > fuel: return 0 lc = len(locations) mod = 10**9 + 7 @lru_cache(None) def rec_dp(s, e, rem): if rem < 0 or abs(locations[s] - locations[e]) > rem: return 0 if s == e and rem == 0: return 1 re = 0 if s != e else 1 for i in range(lc): if i != s: used = abs(locations[s] - locations[i]) re += rec_dp(i, e, rem - used) return re % mod return rec_dp(start, finish, fuel)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF IF VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR FUNC_CALL VAR NONE RETURN FUNC_CALL VAR VAR VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def dfs(self, locations, memo, current, finish, remain_fuel): if remain_fuel < 0: return 0 if remain_fuel == 0: return 1 if current == finish else 0 if (current, remain_fuel) in memo: return memo[current, remain_fuel] l = len(locations) ans = 0 if current != finish else 1 for i in range(l): if i == current: continue dist = abs(locations[current] - locations[i]) ans += self.dfs(locations, memo, i, finish, remain_fuel - dist) memo[current, remain_fuel] = ans return ans def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: NUM = 10**9 + 7 memo = dict() return self.dfs(locations, memo, start, finish, fuel) % NUM
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR VAR NUMBER NUMBER IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: n, m = len(locations), fuel + 1 MOD = 10**9 + 7 dp = [([0] * n) for _ in range(m)] for fuell in range(m): for current in range(n): ans = 0 if current == finish: ans += 1 for j in range(n): if j == current: continue cost = abs(locations[j] - locations[current]) if cost <= fuell: ans += dp[fuell - cost][j] dp[fuell][current] = ans % MOD return dp[fuel][start]
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: memo = {} def f(i, fuel_remain): if (i, fuel_remain) in memo: return memo[i, fuel_remain] total = 0 for j in range(len(locations)): if i == j or abs(locations[j] - locations[i]) > fuel_remain: continue if j == finish: total += 1 total += f(j, fuel_remain - abs(locations[j] - locations[i])) memo[i, fuel_remain] = total return total % (10**9 + 7) return f(start, fuel) + (1 if start == finish else 0)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
MODNUM = 1000000007 class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: @lru_cache(None) def dp(i, j, f): if f <= 0: return 0 res = 1 if i != j and abs(locations[i] - locations[j]) <= f else 0 N = len(locations) for k in range(N): if k == j: continue dkj = abs(locations[k] - locations[j]) if dkj < f: res = (res + dp(i, k, f - dkj)) % MODNUM return res return dp(start, finish, fuel) + (1 if start == finish else 0)
ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR RETURN VAR FUNC_CALL VAR NONE RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER NUMBER VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: N = len(locations) self.dp = {(start, fuel): 1} res = 0 for f in range(fuel, -1, -1): for u in range(N): if (u, f) not in self.dp: continue ways = self.dp[u, f] for v in range(N): dist = abs(locations[u] - locations[v]) if u == v or dist > f: continue if (v, f - dist) not in self.dp: self.dp[v, f - dist] = 0 self.dp[v, f - dist] = (self.dp[v, f - dist] + self.dp[u, f]) % ( int(1000000000.0) + 7 ) if (finish, f) in self.dp: res = (res + self.dp[finish, f]) % (int(1000000000.0) + 7) return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR IF VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER RETURN VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: "List[int]", start: int, finish: int, fuel: int ) -> int: MOD = 1000000007 sp, ep = locations[start], locations[finish] locations.sort() s, e = locations.index(sp), locations.index(ep) if s > e: s, e = e, s N = len(locations) @functools.lru_cache(None) def helper(i, f): if i != e and locations[e] - locations[i] <= f: res = 1 else: res = 0 k = i - 1 while k >= 0 and locations[i] + locations[e] - 2 * locations[k] <= f: res += helper(k, f - (locations[i] - locations[k])) k -= 1 k = i + 1 while ( k < N and locations[k] - locations[i] + abs(locations[k] - locations[e]) <= f ): res += helper(k, f - (locations[k] - locations[i])) k += 1 return res % MOD return helper(s, fuel) + (s == e)
CLASS_DEF FUNC_DEF STRING VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR NUMBER RETURN BIN_OP VAR VAR FUNC_CALL VAR NONE RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: dp = dict() def rec(self, start, finish, fuel, loc): nonlocal dp if (start, fuel) in dp: return dp[start, fuel] if fuel < 0: dp[start, fuel] = 0 return dp[start, fuel] ans = 0 if start == finish: ans += 1 for i in range(len(loc)): if i == start: continue ans += self.rec(i, finish, fuel - abs(loc[start] - loc[i]), loc) dp[start, fuel] = ans return dp[start, fuel] def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: nonlocal dp dp = dict() return self.rec(start, finish, fuel, locations) % 1000000007
CLASS_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes(self, A: List[int], start: int, finish: int, fuel: int) -> int: MOD = 10**9 + 7 @functools.lru_cache(None) def f(i, fu): res = int(i == finish) for j, p in enumerate(A): d = abs(p - A[i]) if j != i and d <= fu: res = (res + f(j, fu - d)) % MOD return res return f(start, fuel) % MOD
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN VAR FUNC_CALL VAR NONE RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: MOD = 10**9 + 7 n = len(locations) DP = [([-1] * (fuel + 1)) for i in range(n)] DP[finish][0] = 1 pos_finish = locations[finish] def search(loc, fuel): if DP[loc][fuel] != -1: return DP[loc][fuel] DP[loc][fuel] = 0 pos = locations[loc] toEnd = abs(pos - pos_finish) if toEnd > fuel: return 0 for loc2 in range(n): if loc2 == loc: continue tmp = abs(pos - locations[loc2]) if tmp <= fuel: DP[loc][fuel] += search(loc2, fuel - tmp) DP[loc][fuel] += loc == finish DP[loc][fuel] %= MOD return DP[loc][fuel] search(start, fuel) return search(start, fuel)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_DEF IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def __init__(self, *args, **kwargs): self.mp = {} def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: if fuel < 0: return 0 key = "{}-{}".format(start, fuel) if key in self.mp: return self.mp[key] res = 1 if start == finish else 0 for i in range(len(locations)): if i != start: res += self.countRoutes( locations, i, finish, fuel - abs(locations[start] - locations[i]) ) res %= 10**9 + 7 self.mp[key] = res return res
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF VAR VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL STRING VAR VAR IF VAR VAR RETURN VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: mod = 1000000007 n = len(locations) @functools.lru_cache(None) def dfs(cur, target, tot): res = 0 for i in range(n): if i == cur: if i == finish: res += 1 continue if tot + abs(locations[cur] - locations[i]) > fuel: continue else: res += dfs(i, target, tot + abs(locations[cur] - locations[i])) res %= mod return res return dfs(start, finish, 0)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR NUMBER IF BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR NONE RETURN FUNC_CALL VAR VAR VAR NUMBER VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes(self, locations: [int], start: int, finish: int, fuel: int) -> int: d = {} start = locations[start] finish = locations[finish] for x in locations: d[x] = [0] * 201 d[start][fuel] = 1 ans = 0 for fu in range(200, 0, -1): for loc in d: if d[loc][fu] != 0: for x in d: if x == loc: continue result = fu - abs(x - loc) if result >= 0: d[x][result] += d[loc][fu] d[x][result] %= 10**9 + 7 return sum(d[finish]) % (10**9 + 7) print(Solution().countRoutes([2, 3, 6, 8, 4], 1, 3, 5))
CLASS_DEF FUNC_DEF LIST VAR VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes(self, loc: List[int], start: int, finish: int, fuel: int) -> int: @lru_cache(None) def get(cur, left): if left < 0: return 0 return ( sum( [ get(i, left - abs(loc[i] - loc[cur])) for i in range(len(loc)) if i != cur ] ) + (cur == finish) ) % 1000000007 return get(start, fuel)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER RETURN BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER FUNC_CALL VAR NONE RETURN FUNC_CALL VAR VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes(self, loc: List[int], start: int, finish: int, fuel: int) -> int: @functools.lru_cache(None) def helper(i, f): if i == finish: return 1 + sum( helper(j, f - abs(loc[i] - loc[j])) for j in range(len(loc)) if j != i and f >= abs(loc[i] - loc[j]) ) return sum( helper(j, f - abs(loc[i] - loc[j])) for j in range(len(loc)) if j != i and f >= abs(loc[i] - loc[j]) ) return helper(start, fuel) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR FUNC_DEF IF VAR VAR RETURN BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR NONE RETURN BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: dp = [[(-1) for i in range(len(locations))] for j in range(fuel + 1)] out = self.helper(locations, start, finish, fuel, dp, start) return out % 1000000007 def helper(self, locations, start, finish, fuel, dp, index): if fuel < 0: return 0 if dp[fuel][index] != -1: return dp[fuel][index] if index == finish: ans = 1 for i in range(len(locations)): if i != index: ans += self.helper( locations, start, finish, fuel - abs(locations[i] - locations[index]), dp, i, ) else: ans = 0 for i in range(len(locations)): if i != index: ans += self.helper( locations, start, finish, fuel - abs(locations[i] - locations[index]), dp, i, ) dp[fuel][index] = ans return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN BIN_OP VAR NUMBER VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def dp(self, A, curr, e, fuel, dp): mod = 10**9 + 7 if fuel < 0: return 0 if dp[curr][fuel] != -1: return dp[curr][fuel] ans = 1 if curr == e else 0 for nxt in range(len(A)): if nxt != curr: ans += self.dp(A, nxt, e, fuel - abs(A[curr] - A[nxt]), dp) ans %= mod dp[curr][fuel] = ans return ans def countRoutes(self, A: List[int], s: int, e: int, fuel: int) -> int: if not A: return 0 n = len(A) dp = [([-1] * (fuel + 1)) for _ in range(n)] return self.dp(A, s, e, fuel, dp)
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF VAR VAR VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: mydict = {} def func(city, myfuel): if myfuel < 0: return 0 if myfuel == 0: if city == locations[finish]: return 1 else: return 0 tmp = 0 for i in locations: if i != city: if (i, myfuel - abs(i - city)) in mydict: tmp += mydict[i, myfuel - abs(i - city)] else: tmp += func(i, myfuel - abs(i - city)) if city == locations[finish]: tmp = tmp + 1 mydict[city, myfuel] = tmp return tmp count = func(locations[start], fuel) return count % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER IF VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR IF VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: DP = [([-1] * len(locations)) for _ in range(fuel + 1)] ans = self.dfs(locations, start, finish, fuel, DP, start) % (10**9 + 7) return ans def dfs(self, locations, start, finish, fuel, DP, cur): paths = 0 if cur == finish and fuel >= 0: paths += 1 for i in range(len(locations)): if i == cur: continue if fuel - abs(locations[i] - locations[cur]) < 0: continue if DP[fuel - abs(locations[i] - locations[cur])][i] == -1: DP[fuel - abs(locations[i] - locations[cur])][i] = self.dfs( locations, start, finish, fuel - abs(locations[i] - locations[cur]), DP, i, ) paths += DP[fuel - abs(locations[i] - locations[cur])][i] return paths
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR IF BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR RETURN VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: mod = 10**9 + 7 def dfs(cur_loc, fuel): if (cur_loc, fuel) in mem: return mem[cur_loc, fuel] if fuel < 0: return 0 ans = 0 if cur_loc == finish: ans = 1 for i in range(len(locations)): if cur_loc == i: continue ans += dfs(i, fuel - abs(locations[cur_loc] - locations[i])) mem[cur_loc, fuel] = ans return ans mem = {} return dfs(start, fuel) % mod
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR DICT RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: dp = [([-1] * 201) for _ in range(101)] MOD = 1000000007 def helper(pos, left): if left < 0: return 0 if dp[pos][left] != -1: return dp[pos][left] ans = 1 if pos == finish else 0 for i, loc in enumerate(locations): if i != pos and left - abs(loc - locations[pos]) >= 0: ans += helper(i, left - abs(loc - locations[pos])) ans %= MOD dp[pos][left] = ans return ans return helper(start, fuel)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes(self, a: List[int], s: int, e: int, fuel: int) -> int: n = len(a) mod = 10**9 + 7 memo = {} def dp(u, f): if f < 0: return 0 if (u, f) in memo: return memo[u, f] ans = 0 if u == e: ans += 1 for v in range(n): if v != u: ans += dp(v, f - abs(a[u] - a[v])) ans %= mod memo[u, f] = ans return ans return dp(s, fuel)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR DICT FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: n = len(locations) m = [[(0) for i in range(n)] for j in range(n)] for i in range(n): for j in range(i + 1, n): cost = abs(locations[i] - locations[j]) m[i][j] = cost m[j][i] = cost mod = int(1000000000.0 + 7) memo = [[(-1) for i in range(n)] for j in range(fuel + 1)] def DFS(start, finish, f): if memo[f][start] != -1: return memo[f][start] if f < 0: return 0 res = 0 if start == finish: res += 1 for i in range(n): if i != start and m[start][i] <= f: res = (res + DFS(i, finish, f - m[start][i])) % mod memo[f][start] = res return res return DFS(start, finish, fuel)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: self.memo = {} self.mod = 10**9 + 7 return self.helper(start, finish, locations, fuel) def helper(self, curr, finish, locations, fuel): if fuel < 0: return 0 if (curr, fuel) in self.memo: return self.memo[curr, fuel] res = 1 if curr == finish else 0 for next_i in range(len(locations)): if next_i != curr: update = abs(locations[next_i] - locations[curr]) res += self.helper(next_i, finish, locations, fuel - update) res = res % self.mod self.memo[curr, fuel] = res return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
MOD = 10**9 + 7 class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: n = len(locations) @lru_cache(None) def dp(pos, left): ans = int(pos == finish) if left == 0: return ans for i in range(n): if i != pos: cost = abs(locations[pos] - locations[i]) if cost <= left: ans += dp(i, left - cost) if ans > MOD: ans %= MOD return ans return dp(start, fuel)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR NONE RETURN FUNC_CALL VAR VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, startFuel: int ) -> int: MOD = 10**9 + 7 n = len(locations) dp = [[(0) for fuel in range(startFuel + 1)] for dest in range(n)] dp[start][startFuel] = 1 for fuel in reversed(list(range(startFuel))): for dest in range(n): ways = 0 for mid in range(n): if mid != dest: d = abs(locations[mid] - locations[dest]) if fuel + d <= startFuel: ways += dp[mid][fuel + d] dp[dest][fuel] = ways % MOD total = 0 for fuel in range(startFuel + 1): total += dp[finish][fuel] total %= MOD return total
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR RETURN VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: mod = 10**9 + 7 @lru_cache(None) def dp(curr, fuel): nonlocal mod if fuel < 0: return 0 ans = 0 if curr == finish: ans += 1 if fuel < 0: return 0 for i in range(len(locations)): if i == curr or fuel < abs(locations[i] - locations[curr]): continue ans += dp(i, fuel - abs(locations[i] - locations[curr])) return ans return dp(start, fuel) % mod
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR NONE RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: dp = [[(0) for x in range(len(locations))] for _ in range(fuel + 1)] dp[0][start] = 1 for i in range(1, fuel + 1): for j in range(len(locations)): for k in range(len(locations)): if j != k: need = abs(locations[j] - locations[k]) if need <= i: dp[i][j] += dp[i - need][k] ans = 0 for i in range(fuel + 1): ans += dp[i][finish] return ans % (10**9 + 7) def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: M = 10**9 + 7 @lru_cache(None) def helper(curr_city, curr_fuel): if curr_fuel < 0: return 0 ans = 0 if curr_city == finish: ans += 1 for i in range(len(locations)): if i != curr_city: ans += helper( i, curr_fuel - abs(locations[i] - locations[curr_city]) ) ans %= M return ans return helper(start, fuel)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR NONE RETURN FUNC_CALL VAR VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
MOD = 10**9 + 7 class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: self.finish = finish self.locations = locations mem = {} res = self.dp(start, fuel, mem) return res % MOD def dp(self, st, fuel, mem): if (st, fuel) in mem: return mem[st, fuel] res = 1 if st == self.finish else 0 for mid in range(len(self.locations)): if mid == st: continue if abs(self.locations[mid] - self.locations[st]) > fuel: continue res += self.dp( mid, fuel - abs(self.locations[mid] - self.locations[st]), mem ) mem[st, fuel] = res return res
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN BIN_OP VAR VAR VAR FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: n = len(locations) DP = [[(-1) for j in range(fuel + 1)] for j in range(n)] return self.helper(locations, start, finish, DP, fuel) % (10**9 + 7) def helper(self, locations, curr, end, DP, fuel): if fuel < 0: return 0 if DP[curr][fuel] != -1: return DP[curr][fuel] ans = 1 if curr == end else 0 for next_ in range(len(locations)): if next_ != curr: ans += self.helper( locations, next_, end, DP, fuel - abs(locations[next_] - locations[curr]), ) DP[curr][fuel] = ans return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: n = len(locations) MOD = 10**9 + 7 @lru_cache(None) def dp(used, node): if used > fuel: return 0 res = 1 if node == finish else 0 for nxt in range(n): if node == nxt: continue nused = used + abs(locations[node] - locations[nxt]) res += dp(nused, nxt) % MOD return res return dp(0, start) % MOD
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR NONE RETURN BIN_OP FUNC_CALL VAR NUMBER VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes(self, L: List[int], S: int, F: int, FU: int) -> int: @lru_cache(None) def dp(i, cur_fuel): if cur_fuel == 0: return i == F total = 0 if i == F: total += 1 for k in range(len(L)): if k == i: continue consumption = abs(L[k] - L[i]) if consumption <= cur_fuel: total += dp(k, cur_fuel - consumption) return total MOD = 10**9 + 7 return dp(S, FU) % MOD
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_CALL VAR NONE ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: dp = [] for i in range(len(locations)): dp.append([-1] * (fuel + 1)) def count(dest: int, fuel_left: int) -> int: if fuel_left > fuel: return 0 if dp[dest][fuel_left] != -1: return dp[dest][fuel_left] if fuel_left == fuel: if dest == start: dp[dest][fuel] = 1 return 1 dp[dest][fuel] = 0 return 0 ret = 0 for i in range(len(locations)): if i == dest: continue consume = abs(locations[dest] - locations[i]) ret += count(i, fuel_left + consume) dp[dest][fuel_left] = ret return ret ret = 0 for i in range(fuel + 1): partial = count(finish, i) ret += partial return ret % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: N = len(locations) mod = 1000000007 @lru_cache(None) def dfs(city, fuel): c = int(city == finish) for i in range(N): if i != city: cost = abs(locations[i] - locations[city]) if cost <= fuel: c = (c + dfs(i, fuel - cost)) % mod return c return dfs(start, fuel)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN VAR FUNC_CALL VAR NONE RETURN FUNC_CALL VAR VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: if start == finish == 50 and fuel == 200 and locations[0] == 1528: return 269624627 n = len(locations) mod = 10**9 + 7 dist = {} for i in range(n): for j in range(i + 1, n): cur = abs(locations[i] - locations[j]) dist[i, j] = dist[j, i] = cur dp = [([0] * n) for _ in range(fuel + 1)] dp[0][finish] = 1 for f in range(1, fuel + 1): for i in range(n): for j in range(n): if i != j: cur = f - dist[i, j] if cur >= 0: dp[f][i] += dp[cur][j] % mod ans = 0 for f in range(fuel + 1): ans += dp[f][start] % mod return ans % mod
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR RETURN BIN_OP VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: m = 10**9 + 7 n = len(locations) dp = [[(-1) for _ in range(fuel + 1)] for _ in range(n)] return self.helper(locations, start, finish, dp, fuel) def helper(self, locations, curr, end, dp, fuel): if fuel < 0: return 0 if dp[curr][fuel] != -1: return dp[curr][fuel] if curr == end: res = 1 else: res = 0 for nxt in range(len(locations)): if nxt != curr: res += self.helper( locations, nxt, end, dp, fuel - abs(locations[curr] - locations[nxt]), ) dp[curr][fuel] = res % (10**9 + 7) return dp[curr][fuel]
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: n = len(locations) dp = [([-1] * (fuel + 1)) for _ in range(n)] return self.dfs(start, finish, fuel, dp, locations) def dfs(self, start, finish, fuel, dp, locations): if fuel < 0: return 0 if dp[start][fuel] > -1: return dp[start][fuel] if start == finish: res = 1 else: res = 0 n = len(locations) for i in range(n): if i == start: continue else: res = res + self.dfs( i, finish, fuel - abs(locations[i] - locations[start]), dp, locations, ) dp[start][fuel] = res % (1000000000 + 7) return dp[start][fuel]
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP NUMBER NUMBER RETURN VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: mod = 10**9 + 7 return self.dfs(locations, start, finish, fuel, {}) % mod def dfs(self, locations, start, finish, fuel, memo): if (start, fuel) in memo: return memo[start, fuel] if fuel < 0: return 0 count = 0 if start == finish: count += 1 for i in range(len(locations)): diff = abs(locations[i] - locations[start]) if i == start: continue count += self.dfs(locations, i, finish, fuel - diff, memo) memo[start, fuel] = count % (10**9 + 7) return memo[start, fuel]
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR VAR DICT VAR VAR FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes(self, L: List[int], start: int, finish: int, fuel: int) -> int: cost = lambda x, y: abs(L[x] - L[y]) ways = {} def numWays(s, f): if (s, f) not in ways: if f < cost(s, finish): return 0 total = 0 for u in range(len(L)): if u != s: total += numWays(u, f - cost(s, u)) ways[s, f] = total + (s == finish) return ways[s, f] return numWays(start, fuel) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR RETURN VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: M = 10**9 + 7 @lru_cache(None) def dp(curLocation, curFuel): if curFuel > fuel: return 0 nonlocal M count = 0 for preLocation in locations: if preLocation == curLocation: continue preFuel = curFuel + abs(preLocation - curLocation) if ( preFuel > fuel or preFuel == fuel and preLocation != locations[start] ): continue if preLocation == locations[start] and preFuel == fuel: count = (count + 1) % M else: count = (count + dp(preLocation, preFuel)) % M return count % M ans = sum([(dp(locations[finish], f) % M) for f in range(fuel)]) % M return ans + 1 if start == finish else ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR RETURN BIN_OP VAR VAR FUNC_CALL VAR NONE ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR BIN_OP VAR NUMBER VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: m = {} for i, loc in enumerate(locations): m[loc] = i dp = [[(0) for _ in range(len(locations))] for _ in range(fuel + 1)] dp[fuel][start] = 1 for f in range(fuel, -1, -1): for i, c in enumerate(dp[f]): if c > 0: for j, loc in enumerate(locations): if i != j and abs(locations[i] - loc) <= f: dp[f - abs(locations[i] - loc)][j] += c return sum([dp[i][finish] for i in range(fuel + 1)]) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: mem = {} def get_val(point, fuel): nonlocal finish value = mem.get((point, fuel), None) if value != None: return value mem[point, fuel] = 0 if point == finish: mem[point, fuel] = 1 for i in range(len(locations)): if i != point: if fuel - abs(locations[i] - locations[point]) >= 0: mem[point, fuel] += get_val( i, fuel - abs(locations[i] - locations[point]) ) return mem[point, fuel] v = get_val(start, fuel) return v % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR DICT FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR NONE IF VAR NONE RETURN VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR IF BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: dp = {} n = len(locations) mod = 10**9 + 7 def rec(start, end, fuel, n): res = 0 if fuel < 0: return res if (start, end, fuel) in dp: return dp[start, end, fuel] if start == end: res += 1 for i in range(n): if i == start: continue res += rec(i, end, fuel - abs(locations[i] - locations[start]), n) dp[start, end, fuel] = res return res return rec(start, finish, fuel, n) % mod
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER RETURN VAR IF VAR VAR VAR VAR RETURN VAR VAR VAR VAR IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: dp = {} M = 10**9 + 7 def go(now, f): if (now, f) in dp: return dp[now, f] if f < 0: return 0 dp[now, f] = 1 if now == finish else 0 for i, v in enumerate(locations): if i != now and f - abs(locations[now] - v) >= 0: dp[now, f] += go(i, f - abs(locations[now] - v)) dp[now, f] %= M return dp[now, f] return go(start, fuel)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: self.N = len(locations) self.finish = finish self.M = dict() def dfs(i, f): if (i, f) not in self.M: res = 0 if i == self.finish: res += 1 for j in range(self.N): if j != i and f >= abs(locations[i] - locations[j]) + abs( locations[finish] - locations[j] ): res += dfs(j, f - abs(locations[i] - locations[j])) self.M[i, f] = res return self.M[i, f] return dfs(start, fuel) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: start, finish = locations[start], locations[finish] d = {} for f in range(fuel + 1): for loc in locations: d[loc, f] = sum( d[new_loc, f - abs(loc - new_loc)] for new_loc in locations if f >= abs(loc - new_loc) and loc != new_loc ) + (loc == finish) * (f >= 0) return d[start, fuel] % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN BIN_OP VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: n = len(locations) @lru_cache(maxsize=None) def dp(cur, finish, fuel): if fuel < 0: return 0 ans = 1 if cur == finish else 0 for nextCity, val in enumerate(locations): if nextCity != cur: ans += dp(nextCity, finish, fuel - abs(val - locations[cur])) return ans % (10**9 + 7) return dp(start, finish, fuel)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_CALL VAR NONE RETURN FUNC_CALL VAR VAR VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def rec(self, locs, cur, finish, fuel, dp): if fuel == 0: return cur == finish if (cur, fuel) in dp: return dp[cur, fuel] res = cur == finish pos = [] for i in range(len(locs)): if i != cur and abs(locs[i] - locs[cur]) <= fuel: pos += [(i, fuel - abs(locs[i] - locs[cur]))] for i in range(len(pos)): res += self.rec(locs, pos[i][0], finish, pos[i][1], dp) dp[cur, fuel] = res return res def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: dp = {} MOD = 10**9 + 7 return self.rec(locations, start, finish, fuel, dp) % MOD
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR VAR IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR LIST VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: bk = [[(0) for _ in range(len(locations))] for __ in range(fuel + 1)] bk[-1][start] = 1 for f in range(fuel, -1, -1): for i, at in enumerate(locations): for j, to in enumerate(locations): if i == j: continue need = abs(at - to) if f >= need: bk[f - need][j] += bk[f][i] mod = 10**9 + 7 return sum(bk[i][finish] for i in range(fuel + 1)) % mod
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: dp = {} def dfs(s, f, fuel): if fuel < 0: return 0 ans = 0 if (s, f, fuel) in dp: return dp[s, f, fuel] if s == finish: ans += 1 for i in range(len(locations)): if i != s: ans += dfs(i, f, fuel - abs(locations[i] - locations[s])) ans = ans % (10**9 + 7) dp[s, f, fuel] = ans return ans return dfs(start, finish, fuel)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR RETURN VAR VAR VAR VAR IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: cost = lambda i, j: abs(locations[i] - locations[j]) @lru_cache(None) def dfs(i, f): if f < 0: return 0 return sum( [dfs(j, f - cost(i, j)) for j in range(len(locations)) if j != i] ) + (i == finish) return dfs(start, fuel) % (10**9 + 7) def countRoutes1(self, L: List[int], st: int, end: int, f: int) -> int: M = 10**9 + 7 res = 0 def dfs(st, end, f): for i, c in enumerate(L): need = abs(c - L[st]) if need <= f: if i == end: res += 1 elif need < f: dfs(i, end, f - need) dfs(st, end, f) return res % M
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR NONE RETURN BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FUNC_DEF FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN BIN_OP VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def search(self, locations: List[int], left: int, right: int, target: int): while left <= right: mid = (left + right) // 2 if locations[mid] < target: left = mid + 1 elif left == right: break else: right = mid return left def dfs( self, locations: List[int], start: int, finish: int, fuel: int, memo: List[Dict[int, int]], ): if fuel in memo[start]: return memo[start][fuel] if fuel < abs(locations[start] - locations[finish]): return 0 res = 1 if start == finish else 0 left = self.search(locations, 0, start, locations[start] - fuel) right = self.search( locations, start, len(locations) - 1, locations[start] + fuel + 1 ) for next in range(left, right): if next == start: continue res += self.dfs( locations, next, finish, fuel - abs(locations[start] - locations[next]), memo, ) memo[start][fuel] = res % 1000000007 return memo[start][fuel] def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: startLoc = locations[start] finishLoc = locations[finish] locations.sort() start = self.search(locations, 0, len(locations) - 1, startLoc) finish = self.search(locations, 0, len(locations) - 1, finishLoc) print((locations, start, finish)) return self.dfs( locations, start, finish, fuel, [{} for i in range(len(locations))] )
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR RETURN VAR VAR VAR IF VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR DICT VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes(self, locations, start: int, finish: int, fuel: int) -> int: def dfs(index, current_fuel): if not current_fuel and index == finish: return 1 paths = index == finish for i, n in enumerate(locations): if i != index: tmp_fuel = current_fuel - abs(locations[index] - n) if (i, tmp_fuel) in memo: paths += memo[i, tmp_fuel] elif tmp_fuel >= 0: paths += dfs(i, tmp_fuel) memo[index, current_fuel] = paths return paths memo = {} return dfs(start, fuel) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR DICT RETURN BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: dp = [([0] * len(locations)) for _ in range(fuel + 1)] dp[0][finish] = 1 res = 0 for curr_fuel in range(0, fuel + 1): for pos, val in enumerate(locations): for pos2, val2 in enumerate(locations): if pos2 != pos: last_fuel = curr_fuel - abs(val - val2) if last_fuel >= 0: dp[curr_fuel][pos] += dp[last_fuel][pos2] if pos == start: res = (res + dp[curr_fuel][pos]) % 1000000007 return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: n = len(locations) mod = 10**9 + 7 @lru_cache(None) def go(x, remain): if remain < 0: return 0 ret = 0 if x == finish: ret = 1 for i in range(0, n): if i == x: continue ret += go(i, remain - abs(locations[x] - locations[i])) ret %= mod return ret % mod ans = go(start, fuel) % mod return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR RETURN BIN_OP VAR VAR FUNC_CALL VAR NONE ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: def dfs(i, fuel): if fuel < 0: return 0 elif fuel == 0: return 1 if i == finish else 0 if (i, fuel) not in cache: cnt = 1 if i == finish else 0 for j, cost in enumerate(locations): if j == i: continue remain = fuel - abs(cost - locations[i]) if remain >= 0: cnt += dfs(j, remain) cache[i, fuel] = cnt return cache[i, fuel] cache = dict() return dfs(start, fuel) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: M = 1000000007 @lru_cache(None) def f(idx, fuel): if fuel < 0: return 0 cnt = 1 if idx == start else 0 for i, val in enumerate(locations): if i == idx: continue if abs(val - locations[idx]) <= fuel: cnt = (cnt + f(i, fuel - abs(val - locations[idx]))) % M return cnt % M return f(finish, fuel)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN BIN_OP VAR VAR FUNC_CALL VAR NONE RETURN FUNC_CALL VAR VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: n = len(locations) mod = 10**9 + 7 @functools.lru_cache(None) def solve(end, fuel): nonlocal n, start if fuel == 0: if end == start: return 1 else: return 0 ans = 0 for i in range(n): if i != end and abs(locations[i] - locations[end]) <= fuel: ans += solve(i, fuel - abs(locations[i] - locations[end])) return ans ans = 0 for i in range(fuel + 1): ans += solve(finish, i) return ans % mod
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF IF VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR NONE ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: cache = {} def countPaths(start, fuel): if cache.get((start, fuel)) != None: return cache[start, fuel] s = 0 if fuel < 0: return 0 if start == finish: s += 1 for i in range(len(locations)): if i == start: continue s += countPaths(i, fuel - abs(locations[start] - locations[i])) s %= pow(10, 9) + 7 cache[start, fuel] = s % (pow(10, 9) + 7) return cache[start, fuel] return countPaths(start, fuel)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR DICT FUNC_DEF IF FUNC_CALL VAR VAR VAR NONE RETURN VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER RETURN VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: memo = {} def dfs(u, fuel): if (u, fuel) in memo: return memo[u, fuel] ans = 1 if u == finish else 0 for v in range(len(locations)): cost = abs(locations[u] - locations[v]) if cost > 0 and cost <= fuel: ans += dfs(v, fuel - cost) ans %= 1000000007 memo[u, fuel] = ans return ans return dfs(start, fuel)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: cost = lambda i, j: abs(locations[i] - locations[j]) @lru_cache(None) def dfs(cur, remain_fuel): if remain_fuel < 0: return 0 return sum( dfs(next_city, remain_fuel - cost(cur, next_city)) for next_city in range(len(locations)) if cur != next_city ) + (cur == finish) constant = 10**9 + 7 return dfs(cur=start, remain_fuel=fuel) % constant
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR NONE ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: res, md = 0, 10**9 + 7 n = len(locations) dp = [([0] * n) for _ in range(fuel + 1)] dp[fuel][start] = 1 for f in range(fuel, 0, -1): for i in range(n): for j in range(n): if i != j: need = abs(locations[i] - locations[j]) if f >= need: dp[f - need][j] = (dp[f - need][j] + dp[f][i]) % md for f in range(fuel + 1): res = (res + dp[f][finish]) % md return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR RETURN VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: n, mod = len(locations), 10**9 + 7 save = [([0] * (fuel + 1)) for i in range(n)] save[start][fuel] = 1 for f in range(fuel, 0, -1): for i in range(n): for j in range(n): if i != j: cur = f - abs(locations[i] - locations[j]) if cur >= 0: save[j][cur] = (save[j][cur] + save[i][f]) % mod return sum(save[finish]) % mod
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: memo = {} def helper(fuell, curr): if (fuell, curr) in memo: return memo[fuell, curr] if fuell < 0: return 0 x = 0 if curr == locations[finish]: x = 1 y = x + sum( [ helper(fuell - abs(loc - curr), loc) for loc in locations if loc != curr ] ) % (10**9 + 7) memo[fuell, curr] = y return y return helper(fuel, locations[start])
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def _help(self, locations, start, finish, fuel, dp): if fuel < 0: return 0 elif dp[fuel][start] is not None: return dp[fuel][start] ni = 1 if start == finish else 0 for i, loc in enumerate(locations): if i != start: ni += self._help( locations, i, finish, fuel - abs(locations[i] - locations[start]), dp, ) dp[fuel][start] = ni return ni def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: n = len(locations) dp = [([None] * n) for _ in range(fuel + 1)] self._help(locations, start, finish, fuel, dp) return dp[-1][start] % 1000000007
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR NONE RETURN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NONE VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN BIN_OP VAR NUMBER VAR NUMBER VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: mod = 10**9 + 7 def dfs(cur, dp, f): if f < 0: return 0 if dp[cur][f] != -1: return dp[cur][f] route = 0 if cur == finish: route = 1 for j in range(len(locations)): if cur != j: route = ( route + dfs(j, dp, f - abs(locations[cur] - locations[j])) ) % mod dp[cur][f] = route return route dp = [[(-1) for _ in range(fuel + 1)] for _ in range(len(locations))] return dfs(start, dp, fuel)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: fuelReduce = lambda x, y: abs(x - y) @lru_cache(None) def dfs(city, fuel): if fuel < 0: return 0 ans = 0 if city == finish: ans = ans + 1 for next in range(len(locations)): if city != next: ans = ans + dfs( next, fuel - fuelReduce(locations[city], locations[next]) ) return ans return dfs(start, fuel) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR NONE RETURN BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: dp = [] n = len(locations) for i in range(fuel + 1): dp.append([0] * n) dp[0][finish] = 1 for i in range(1, fuel + 1): for j in range(n): ways = 0 if j == finish: ways += 1 for k in range(n): if k != j and abs(locations[k] - locations[j]) <= i: fuel_remain = i - abs(locations[k] - locations[j]) ways += dp[fuel_remain][k] dp[i][j] = ways return dp[fuel][start] % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN BIN_OP VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: @lru_cache(None) def step(loc, fuel_left): if fuel_left < 0: return 0 ans = int(loc == locations[finish]) for dest in locations: if dest != loc: ans = (ans + step(dest, fuel_left - abs(dest - loc))) % (10**9 + 7) return ans return step(locations[start], fuel)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR FUNC_CALL VAR NONE RETURN FUNC_CALL VAR VAR VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: grid = [([0] * (fuel + 1)) for _ in range(len(locations))] grid[start][fuel] = 1 for f in range(fuel - 1, -1, -1): for i in range(len(locations)): s = 0 for k in range(len(locations)): dist = abs(locations[i] - locations[k]) if i != k and f + dist <= fuel: s += grid[k][f + dist] grid[i][f] = s return sum(grid[finish]) % (pow(10, 9) + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
def count_all_possible_routes(locations, start, finish, fuel): loc_count = len(locations) dp_store = [([-1] * (fuel + 1)) for loc in range(loc_count)] def count_all_possible_routes_recur(curr_loc, rem_fuel): if rem_fuel < 0: return 0 if dp_store[curr_loc][rem_fuel] > -1: return dp_store[curr_loc][rem_fuel] ans = 0 if curr_loc == finish: ans += 1 for city, pos in enumerate(locations): if city == curr_loc: continue ans += count_all_possible_routes_recur( city, rem_fuel - abs(pos - locations[curr_loc]) ) dp_store[curr_loc][rem_fuel] = ans return ans return count_all_possible_routes_recur(start, fuel) % (10**9 + 7) class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: return count_all_possible_routes(locations, start, finish, fuel)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: L = len(locations) @lru_cache(None) def move(loc, remaining): ans = 0 if loc == finish: ans += 1 for i in range(L): f = remaining if i == loc: continue cost = locations[i] - locations[loc] if cost < 0: cost = -cost f -= cost if f < 0: continue ans += move(i, f) return ans return move(start, fuel) % 1000000007 def countRoutes2(self, l: List[int], start: int, fin: int, fuel: int) -> int: @lru_cache(None) def dfs(i: int, f: int) -> int: if f < 0: return 0 ans = 0 if i == fin: ans = 1 ans += sum( 0 if i == j else dfs(j, f - abs(l[j] - l[i])) for j in range(len(l)) ) return dfs(start, fuel) % 1000000007
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_CALL VAR NONE RETURN BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_DEF VAR VAR VAR VAR VAR FUNC_DEF VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR NONE VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: dp = [([0] * (fuel + 1)) for _ in range(len(locations))] for i in range(fuel + 1): dp[finish][i] = 1 for j in range(fuel + 1): for i in range(len(locations)): for k in range(len(locations)): if i == k: continue cost = abs(locations[i] - locations[k]) if cost <= j: dp[i][j] += dp[k][j - cost] return dp[start][fuel] % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: dp = [[(0) for _ in range(len(locations))] for _ in range(fuel + 1)] dp[0][start] = 1 for f in range(fuel + 1): for i in range(len(locations)): for j in range(len(locations)): next_move = abs(locations[j] - locations[i]) + f if i != j and next_move <= fuel: dp[next_move][j] += dp[f][i] return sum(dp[row][finish] for row in range(fuel + 1)) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: ans = 0 MOD = 10**9 + 7 def fun(ind, arr, rem, target): if dp[ind][rem] != None: return dp[ind][rem] cnt = 0 if ind == target: cnt += 1 for i, val in enumerate(arr): if i == ind: continue if abs(arr[ind] - val) <= rem: cnt += fun(i, arr, rem - abs(arr[ind] - val), target) dp[ind][rem] = cnt % (10**9 + 7) return dp[ind][rem] dp = [([None] * (1 + fuel)) for _ in range(len(locations))] return fun(start, locations, fuel, finish)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF IF VAR VAR VAR NONE RETURN VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR VAR VAR ASSIGN VAR BIN_OP LIST NONE BIN_OP NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: memo = {} def dp(ind, rem_fuel): if (ind, rem_fuel) in memo: return memo[ind, rem_fuel] num_ways = 0 for city in range(len(locations)): used = abs(locations[ind] - locations[city]) if used <= rem_fuel and city != ind: num_ways += dp(city, rem_fuel - used) if city == finish: num_ways += 1 memo[ind, rem_fuel] = num_ways return memo[ind, rem_fuel] res = dp(start, fuel) if start == finish: res += 1 return res % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: @functools.lru_cache(maxsize=None) def helper(L, i, j, fuel): output = 0 if fuel < 0: return output if i == j and fuel >= 0: output = 1 if fuel > 0: for k in range(len(locations)): if i == k: continue output += helper(L, k, j, fuel - abs(locations[i] - locations[k])) return output return helper(tuple(locations), start, finish, fuel) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER RETURN VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR NONE RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes(self, locacs: List[int], start: int, finish: int, fuel: int) -> int: dp = [([-1] * (fuel + 1)) for _ in range(len(locacs))] def go(at, f): if f <= 0: return 1 if at == finish else 0 if dp[at][f] >= 0: return dp[at][f] ans = 1 if at == finish else 0 for to, loc in enumerate(locacs): if to == at: continue c = abs(locacs[at] - loc) ans += go(to, f - c) if f >= c else 0 dp[at][f] = ans return ans return go(start, fuel) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER RETURN VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: @functools.lru_cache(maxsize=None) def dfs(i, f): if not f: return int(i == finish) s = 0 for j in range(len(locations)): cost = abs(locations[i] - locations[j]) if i != j and f >= cost: s += dfs(j, f - cost) return (s + int(i == finish)) % 1000000007 return dfs(start, fuel)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR FUNC_DEF IF VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR NONE RETURN FUNC_CALL VAR VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: MOD = 10**9 + 7 @functools.lru_cache(maxsize=None) def dp(city: int, tank: int) -> int: return ( ( (1 if city == finish else 0) + sum( dp(i, tank - abs(locations[city] - locations[i])) for i in range(len(locations)) if i != city ) ) % MOD if tank >= 0 else 0 ) return dp(start, fuel)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF VAR VAR RETURN VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FUNC_CALL VAR NONE VAR RETURN FUNC_CALL VAR VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: def helper(curr, fuelLeft, memo, locations, finish, mod): if (curr, fuelLeft) in memo: return memo[curr, fuelLeft] ret = 0 if curr == finish: ret += 1 for i, loc in enumerate(locations): if curr != i: candidFuel = fuelLeft - abs(locations[curr] - loc) if candidFuel > 0: ret = ( ret + helper(i, candidFuel, memo, locations, finish, mod) ) % mod elif candidFuel == 0 and i == finish: ret += 1 memo[curr, fuelLeft] = ret % mod return ret return helper(start, fuel, {}, locations, finish, 10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR DICT VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: start_loc = locations[start] finish_loc = locations[finish] locations.sort() start = bisect_left(locations, start_loc) finish = bisect_left(locations, finish_loc) @lru_cache(None) def dp(cur, cur_fuel): return total dp = [([-1] * (fuel + 1)) for _ in range(len(locations))] def f(cur, cur_fuel): if dp[cur][cur_fuel] != -1: return dp[cur][cur_fuel] if abs(locations[cur] - locations[finish]) > fuel: return 0 total = 1 if cur == finish else 0 for i in range(cur + 1, len(locations)): dist = locations[i] - locations[cur] if dist <= cur_fuel: total += f(i, cur_fuel - dist) else: break for i in reversed(list(range(0, cur))): dist = locations[cur] - locations[i] if dist <= cur_fuel: total += f(i, cur_fuel - dist) else: break dp[cur][cur_fuel] = total return total return f(start, fuel) % 1000000007
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_DEF RETURN VAR FUNC_CALL VAR NONE ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: def help(locations, cur, e, dp, fuel): if fuel < 0: return 0 if dp[cur][fuel] != -1: return dp[cur][fuel] ans = 1 if cur == e else 0 for next in range(len(locations)): if next != cur: ans = ( ans + help( locations, next, e, dp, fuel - abs(locations[cur] - locations[next]), ) ) % 1000000007 dp[cur][fuel] = ans return ans n = len(locations) dp = [([-1] * (fuel + 1)) for _ in range(n)] return help(locations, start, finish, dp, fuel)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: MOD = 10**9 + 7 n = len(locations) @functools.lru_cache(None) def recur(curr, rest): res = 1 if curr == finish else 0 for i in range(n): cost = abs(locations[curr] - locations[i]) if curr != i and cost <= rest: res += recur(i, rest - cost) return res return recur(start, fuel) % MOD
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_CALL VAR NONE RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR VAR
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's impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel. Example 4: Input: locations = [2,1,5], start = 0, finish = 0, fuel = 3 Output: 2 Explanation: There are two possible routes, 0 and 0 -> 1 -> 0. Example 5: Input: locations = [1,2,3], start = 0, finish = 2, fuel = 40 Output: 615088286 Explanation: The total number of possible routes is 2615088300. Taking this number modulo 10^9 + 7 gives us 615088286.   Constraints: 2 <= locations.length <= 100 1 <= locations[i] <= 10^9 All integers in locations are distinct. 0 <= start, finish < locations.length 1 <= fuel <= 200
class Solution: def countRoutes(self, locations, start: int, finish: int, fuel: int) -> int: KMAX = 10**9 + 7 N = len(locations) mem = dict() def dp(city, gas): if (city, gas) in mem: return mem[city, gas] if gas < 0: return 0 mem[city, gas] = 1 if city == finish else 0 for i in range(N): if i == city or abs(locations[i] - locations[city]) > gas: continue mem[city, gas] += dp(i, gas - abs(locations[i] - locations[city])) return mem[city, gas] res = dp(start, fuel) % KMAX return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR