description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given a square grid of integers arr, a falling path with non-zero shifts is a choice of exactly one element from each row of arr, such that no two elements chosen in adjacent rows are in the same column. Return the minimum sum of a falling path with non-zero shifts.   Example 1: Input: arr = [[1,2,3],[4,5,6],[7,8,9]] Output: 13 Explanation: The possible falling paths are: [1,5,9], [1,5,7], [1,6,7], [1,6,8], [2,4,8], [2,4,9], [2,6,7], [2,6,8], [3,4,8], [3,4,9], [3,5,7], [3,5,9] The falling path with the smallest sum is [1,5,7], so the answer is 13.   Constraints: 1 <= arr.length == arr[i].length <= 200 -99 <= arr[i][j] <= 99
class Solution: def minFallingPathSum(self, cost: List[List[int]]) -> int: cols = len(cost[0]) dp = cost[0] for h in range(1, len(cost)): dp = [ ( cost[h][m] + min(dp[prevMat] for prevMat in range(0, cols) if prevMat != m) ) for m in range(0, cols) ] return min(dp)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR
Given a square grid of integers arr, a falling path with non-zero shifts is a choice of exactly one element from each row of arr, such that no two elements chosen in adjacent rows are in the same column. Return the minimum sum of a falling path with non-zero shifts.   Example 1: Input: arr = [[1,2,3],[4,5,6],[7,8,9]] Output: 13 Explanation: The possible falling paths are: [1,5,9], [1,5,7], [1,6,7], [1,6,8], [2,4,8], [2,4,9], [2,6,7], [2,6,8], [3,4,8], [3,4,9], [3,5,7], [3,5,9] The falling path with the smallest sum is [1,5,7], so the answer is 13.   Constraints: 1 <= arr.length == arr[i].length <= 200 -99 <= arr[i][j] <= 99
class Solution: def minFallingPathSum(self, A): m, n = len(A), len(A[0]) dp = A[0].copy() for x in range(1, m): tmp = [0] * n for y in range(n): tmp[y] = min(dp[py] for py in range(n) if y != py) + A[x][y] dp, tmp = tmp, dp return min(dp)
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR
Given a square grid of integers arr, a falling path with non-zero shifts is a choice of exactly one element from each row of arr, such that no two elements chosen in adjacent rows are in the same column. Return the minimum sum of a falling path with non-zero shifts.   Example 1: Input: arr = [[1,2,3],[4,5,6],[7,8,9]] Output: 13 Explanation: The possible falling paths are: [1,5,9], [1,5,7], [1,6,7], [1,6,8], [2,4,8], [2,4,9], [2,6,7], [2,6,8], [3,4,8], [3,4,9], [3,5,7], [3,5,9] The falling path with the smallest sum is [1,5,7], so the answer is 13.   Constraints: 1 <= arr.length == arr[i].length <= 200 -99 <= arr[i][j] <= 99
class Solution: def minFallingPathSum(self, arr: List[List[int]]) -> int: m = len(arr) print(m) if m == 1: return min(arr[0]) dp = [[(0) for x in range(m)] for x in range(m)] for i in range(m): for j in range(m): if i == 0: dp[i][j] = arr[i][j] else: minn = 1000 for k in range(0, m): if k != j and dp[i - 1][k] < minn: minn = dp[i - 1][k] dp[i][j] = minn + arr[i][j] print(dp) return min(dp[m - 1])
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR
Given an array of integers cost and an integer target. Return the maximum integer you can paint under the following rules: The cost of painting a digit (i+1) is given by cost[i] (0 indexed). The total cost used must be equal to target. Integer does not have digits 0. Since the answer may be too large, return it as string. If there is no way to paint any integer given the condition, return "0".   Example 1: Input: cost = [4,3,2,5,6,7,2,5,5], target = 9 Output: "7772" Explanation: The cost to paint the digit '7' is 2, and the digit '2' is 3. Then cost("7772") = 2*3+ 3*1 = 9. You could also paint "977", but "7772" is the largest number. Digit cost 1 -> 4 2 -> 3 3 -> 2 4 -> 5 5 -> 6 6 -> 7 7 -> 2 8 -> 5 9 -> 5 Example 2: Input: cost = [7,6,5,5,5,6,8,7,8], target = 12 Output: "85" Explanation: The cost to paint the digit '8' is 7, and the digit '5' is 5. Then cost("85") = 7 + 5 = 12. Example 3: Input: cost = [2,4,6,2,4,6,4,4,4], target = 5 Output: "0" Explanation: It's not possible to paint any integer with total cost equal to target. Example 4: Input: cost = [6,10,15,40,40,40,40,40,40], target = 47 Output: "32211"   Constraints: cost.length == 9 1 <= cost[i] <= 5000 1 <= target <= 5000
class Solution: def largestNumber(self, cost: List[int], target: int) -> str: cache = {} def helper(remaining): if remaining == 0: return "" if remaining in cache: return cache[remaining] maximum = "0" for i in range(len(cost)): if cost[i] > remaining: continue result = helper(remaining - cost[i]) if result == "0": continue maximum = larger(maximum, str(i + 1) + result) cache[remaining] = maximum return maximum def larger(a, b): if len(a) > len(b): return a if len(a) < len(b): return b return max(a, b) return helper(target)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR NUMBER RETURN STRING IF VAR VAR RETURN VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given an array of integers cost and an integer target. Return the maximum integer you can paint under the following rules: The cost of painting a digit (i+1) is given by cost[i] (0 indexed). The total cost used must be equal to target. Integer does not have digits 0. Since the answer may be too large, return it as string. If there is no way to paint any integer given the condition, return "0".   Example 1: Input: cost = [4,3,2,5,6,7,2,5,5], target = 9 Output: "7772" Explanation: The cost to paint the digit '7' is 2, and the digit '2' is 3. Then cost("7772") = 2*3+ 3*1 = 9. You could also paint "977", but "7772" is the largest number. Digit cost 1 -> 4 2 -> 3 3 -> 2 4 -> 5 5 -> 6 6 -> 7 7 -> 2 8 -> 5 9 -> 5 Example 2: Input: cost = [7,6,5,5,5,6,8,7,8], target = 12 Output: "85" Explanation: The cost to paint the digit '8' is 7, and the digit '5' is 5. Then cost("85") = 7 + 5 = 12. Example 3: Input: cost = [2,4,6,2,4,6,4,4,4], target = 5 Output: "0" Explanation: It's not possible to paint any integer with total cost equal to target. Example 4: Input: cost = [6,10,15,40,40,40,40,40,40], target = 47 Output: "32211"   Constraints: cost.length == 9 1 <= cost[i] <= 5000 1 <= target <= 5000
class Solution: def largestNumber(self, cost: List[int], target: int) -> str: cost_to_digit = {val: str(index + 1) for index, val in enumerate(cost)} cost = sorted(cost_to_digit.keys()) @lru_cache(None) def helper(target: int) -> str: if target < cost[0]: return None max_number = None if target not in cost_to_digit else cost_to_digit[target] for c in cost: if c > target: break curr_number = helper(target - c) if curr_number is None: continue curr_number = "".join( sorted(cost_to_digit[c] + curr_number, reverse=True) ) max_number = ( curr_number if max_number is None or len(curr_number) > len(max_number) or len(curr_number) == len(max_number) and curr_number > max_number else max_number ) return max_number ans = helper(target) return ans if ans else "0"
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF VAR IF VAR VAR NUMBER RETURN NONE ASSIGN VAR VAR VAR NONE VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NONE ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR NONE FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR NONE VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR VAR STRING VAR
Given an array of integers cost and an integer target. Return the maximum integer you can paint under the following rules: The cost of painting a digit (i+1) is given by cost[i] (0 indexed). The total cost used must be equal to target. Integer does not have digits 0. Since the answer may be too large, return it as string. If there is no way to paint any integer given the condition, return "0".   Example 1: Input: cost = [4,3,2,5,6,7,2,5,5], target = 9 Output: "7772" Explanation: The cost to paint the digit '7' is 2, and the digit '2' is 3. Then cost("7772") = 2*3+ 3*1 = 9. You could also paint "977", but "7772" is the largest number. Digit cost 1 -> 4 2 -> 3 3 -> 2 4 -> 5 5 -> 6 6 -> 7 7 -> 2 8 -> 5 9 -> 5 Example 2: Input: cost = [7,6,5,5,5,6,8,7,8], target = 12 Output: "85" Explanation: The cost to paint the digit '8' is 7, and the digit '5' is 5. Then cost("85") = 7 + 5 = 12. Example 3: Input: cost = [2,4,6,2,4,6,4,4,4], target = 5 Output: "0" Explanation: It's not possible to paint any integer with total cost equal to target. Example 4: Input: cost = [6,10,15,40,40,40,40,40,40], target = 47 Output: "32211"   Constraints: cost.length == 9 1 <= cost[i] <= 5000 1 <= target <= 5000
class Solution: def largestNumber(self, cost: List[int], target: int) -> str: for i in range(len(cost)): for j in range(len(cost) - 1, i, -1): if cost[j] and not cost[i] % cost[j]: cost[i] = 0 break costMap = {e: (k + 1) for k, e in enumerate(cost) if e} print((costMap, cost)) def getMaxList(s1, s2): if s2 == None: return "" return "".join(sorted(s1 + s2, reverse=True)) def dfs(c): if c in dp: return dp[c] if c == 0: return "" elif c < 0: return None strs = [ getMaxList(str(i), dfs(c - cost[i - 1])) for i in range(1, 10) if cost[i - 1] in costMap ] if not any(strs): dp[c] = None return None l = max(list(map(len, strs))) newStr = sorted(strs) for i in range(len(strs) - 1, -1, -1): if len(newStr[i]) == l: dp[c] = newStr[i] return dp[c] dp = {} res = dfs(target) return res if res else "0"
CLASS_DEF FUNC_DEF VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NONE RETURN STRING RETURN FUNC_CALL STRING FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_DEF IF VAR VAR RETURN VAR VAR IF VAR NUMBER RETURN STRING IF VAR NUMBER RETURN NONE ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR ASSIGN VAR VAR NONE RETURN NONE ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR VAR STRING VAR
Given an array of integers cost and an integer target. Return the maximum integer you can paint under the following rules: The cost of painting a digit (i+1) is given by cost[i] (0 indexed). The total cost used must be equal to target. Integer does not have digits 0. Since the answer may be too large, return it as string. If there is no way to paint any integer given the condition, return "0".   Example 1: Input: cost = [4,3,2,5,6,7,2,5,5], target = 9 Output: "7772" Explanation: The cost to paint the digit '7' is 2, and the digit '2' is 3. Then cost("7772") = 2*3+ 3*1 = 9. You could also paint "977", but "7772" is the largest number. Digit cost 1 -> 4 2 -> 3 3 -> 2 4 -> 5 5 -> 6 6 -> 7 7 -> 2 8 -> 5 9 -> 5 Example 2: Input: cost = [7,6,5,5,5,6,8,7,8], target = 12 Output: "85" Explanation: The cost to paint the digit '8' is 7, and the digit '5' is 5. Then cost("85") = 7 + 5 = 12. Example 3: Input: cost = [2,4,6,2,4,6,4,4,4], target = 5 Output: "0" Explanation: It's not possible to paint any integer with total cost equal to target. Example 4: Input: cost = [6,10,15,40,40,40,40,40,40], target = 47 Output: "32211"   Constraints: cost.length == 9 1 <= cost[i] <= 5000 1 <= target <= 5000
def cmp(s, t): if len(s) != len(t): return len(s) < len(t) for i in range(len(s)): if s[i] != t[i]: return s[i] < t[i] return False class Solution: def largestNumber(self, cost: List[int], target: int) -> str: best = {} for i in range(len(cost) - 1, -1, -1): digit = i + 1 if cost[i] not in best: best[cost[i]] = digit keys = sorted(list(best.keys()), key=lambda x: best[x], reverse=True) @lru_cache(None) def f(t, idx): if t == 0: return "" if t < 0 or idx == len(keys): return "0" k = keys[idx] b = "0" best_ct = 0 for ct in range(t // k, -1, -1): r = f(t - k * ct, idx + 1) if r != "0": if b == "0" or ct + len(r) > best_ct + len(b): b = r best_ct = ct return str(best[k]) * best_ct + b return f(target, 0)
FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN VAR VAR VAR VAR RETURN NUMBER CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN STRING IF VAR NUMBER VAR FUNC_CALL VAR VAR RETURN STRING ASSIGN VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR STRING IF VAR STRING BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR NONE RETURN FUNC_CALL VAR VAR NUMBER VAR
Given an array of integers cost and an integer target. Return the maximum integer you can paint under the following rules: The cost of painting a digit (i+1) is given by cost[i] (0 indexed). The total cost used must be equal to target. Integer does not have digits 0. Since the answer may be too large, return it as string. If there is no way to paint any integer given the condition, return "0".   Example 1: Input: cost = [4,3,2,5,6,7,2,5,5], target = 9 Output: "7772" Explanation: The cost to paint the digit '7' is 2, and the digit '2' is 3. Then cost("7772") = 2*3+ 3*1 = 9. You could also paint "977", but "7772" is the largest number. Digit cost 1 -> 4 2 -> 3 3 -> 2 4 -> 5 5 -> 6 6 -> 7 7 -> 2 8 -> 5 9 -> 5 Example 2: Input: cost = [7,6,5,5,5,6,8,7,8], target = 12 Output: "85" Explanation: The cost to paint the digit '8' is 7, and the digit '5' is 5. Then cost("85") = 7 + 5 = 12. Example 3: Input: cost = [2,4,6,2,4,6,4,4,4], target = 5 Output: "0" Explanation: It's not possible to paint any integer with total cost equal to target. Example 4: Input: cost = [6,10,15,40,40,40,40,40,40], target = 47 Output: "32211"   Constraints: cost.length == 9 1 <= cost[i] <= 5000 1 <= target <= 5000
class Solution: def largestNumber(self, cost: List[int], target: int) -> str: d = {i: (-1) for i in cost} for i in range(1, len(cost) + 1): d[cost[i - 1]] = max(d[cost[i - 1]], i) dp = ["-1"] * (target + 1) cost = list(set(cost)) cost.sort() for i in d: try: dp[i] = str(d[i]) except: pass for i in range(1, target + 1): for j in cost: if i - j > 0: if dp[i - j] != "-1": dp[i] = str(max(int(dp[i - j] + str(d[j])), int(dp[i]))) else: try: dp[i] = str(max(int(dp[i]), d[i])) except: pass else: break if dp[target] == "-1": return "0" return dp[target]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST STRING BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR STRING RETURN STRING RETURN VAR VAR VAR
Given an array of integers cost and an integer target. Return the maximum integer you can paint under the following rules: The cost of painting a digit (i+1) is given by cost[i] (0 indexed). The total cost used must be equal to target. Integer does not have digits 0. Since the answer may be too large, return it as string. If there is no way to paint any integer given the condition, return "0".   Example 1: Input: cost = [4,3,2,5,6,7,2,5,5], target = 9 Output: "7772" Explanation: The cost to paint the digit '7' is 2, and the digit '2' is 3. Then cost("7772") = 2*3+ 3*1 = 9. You could also paint "977", but "7772" is the largest number. Digit cost 1 -> 4 2 -> 3 3 -> 2 4 -> 5 5 -> 6 6 -> 7 7 -> 2 8 -> 5 9 -> 5 Example 2: Input: cost = [7,6,5,5,5,6,8,7,8], target = 12 Output: "85" Explanation: The cost to paint the digit '8' is 7, and the digit '5' is 5. Then cost("85") = 7 + 5 = 12. Example 3: Input: cost = [2,4,6,2,4,6,4,4,4], target = 5 Output: "0" Explanation: It's not possible to paint any integer with total cost equal to target. Example 4: Input: cost = [6,10,15,40,40,40,40,40,40], target = 47 Output: "32211"   Constraints: cost.length == 9 1 <= cost[i] <= 5000 1 <= target <= 5000
class Solution: def largestNumber(self, cost: List[int], target: int) -> str: dp = [0] for i in range(1, target + 1): result = 0 for j, c in enumerate(cost): if i < c: continue if dp[i - c] == 0 and i != c: continue result = max(result, 10 * dp[i - c] + j + 1) dp.append(result) return str(dp[-1])
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR
Given an array of integers cost and an integer target. Return the maximum integer you can paint under the following rules: The cost of painting a digit (i+1) is given by cost[i] (0 indexed). The total cost used must be equal to target. Integer does not have digits 0. Since the answer may be too large, return it as string. If there is no way to paint any integer given the condition, return "0".   Example 1: Input: cost = [4,3,2,5,6,7,2,5,5], target = 9 Output: "7772" Explanation: The cost to paint the digit '7' is 2, and the digit '2' is 3. Then cost("7772") = 2*3+ 3*1 = 9. You could also paint "977", but "7772" is the largest number. Digit cost 1 -> 4 2 -> 3 3 -> 2 4 -> 5 5 -> 6 6 -> 7 7 -> 2 8 -> 5 9 -> 5 Example 2: Input: cost = [7,6,5,5,5,6,8,7,8], target = 12 Output: "85" Explanation: The cost to paint the digit '8' is 7, and the digit '5' is 5. Then cost("85") = 7 + 5 = 12. Example 3: Input: cost = [2,4,6,2,4,6,4,4,4], target = 5 Output: "0" Explanation: It's not possible to paint any integer with total cost equal to target. Example 4: Input: cost = [6,10,15,40,40,40,40,40,40], target = 47 Output: "32211"   Constraints: cost.length == 9 1 <= cost[i] <= 5000 1 <= target <= 5000
def cmp(a, b): ad, bd = {}, {} for i in range(0, len(a), 2): ad[a[i]] = a[i + 1] for i in range(0, len(b), 2): bd[b[i]] = b[i + 1] sad = sum(ad.values()) sbd = sum(bd.values()) if sad != sbd: return sad < sbd al = sorted(list(ad.keys()), reverse=True) bl = sorted(list(bd.keys()), reverse=True) for i in range(1 + min(len(al), len(bl))): if al[i] == bl[i]: c = al[i] if ad[c] != bd[c]: return ad[c] < bd[c] else: return al[i] < bl[i] return False class Solution: def largestNumber(self, cost: List[int], target: int) -> str: best = {} for i in range(len(cost) - 1, -1, -1): digit = i + 1 if cost[i] not in best: best[cost[i]] = digit x = sorted(best.keys()) y = sorted(list(best.values()), reverse=True) @lru_cache(None) def best_path(t, idx): if t == 0: return tuple() if idx == len(x): return None ret = None bi = -1 for i in range(t // x[idx], -1, -1): y = best_path(t - i * x[idx], idx + 1) if y is not None: if i > 0: y = (best[x[idx]], i) + y if bi == -1 or cmp(ret, y): ret = y bi = i return ret r = best_path(target, 0) if r is None: return "0" unpacked = {} for i in range(0, len(r), 2): unpacked[r[i]] = r[i + 1] s = "" for digit in y: if digit in unpacked: s += str(digit) * unpacked[digit] return s
FUNC_DEF ASSIGN VAR VAR DICT DICT FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR RETURN VAR VAR VAR VAR RETURN VAR VAR VAR VAR RETURN NUMBER CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR RETURN NONE ASSIGN VAR NONE ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NONE IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_CALL VAR NONE ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NONE RETURN STRING ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
Given an array of integers cost and an integer target. Return the maximum integer you can paint under the following rules: The cost of painting a digit (i+1) is given by cost[i] (0 indexed). The total cost used must be equal to target. Integer does not have digits 0. Since the answer may be too large, return it as string. If there is no way to paint any integer given the condition, return "0".   Example 1: Input: cost = [4,3,2,5,6,7,2,5,5], target = 9 Output: "7772" Explanation: The cost to paint the digit '7' is 2, and the digit '2' is 3. Then cost("7772") = 2*3+ 3*1 = 9. You could also paint "977", but "7772" is the largest number. Digit cost 1 -> 4 2 -> 3 3 -> 2 4 -> 5 5 -> 6 6 -> 7 7 -> 2 8 -> 5 9 -> 5 Example 2: Input: cost = [7,6,5,5,5,6,8,7,8], target = 12 Output: "85" Explanation: The cost to paint the digit '8' is 7, and the digit '5' is 5. Then cost("85") = 7 + 5 = 12. Example 3: Input: cost = [2,4,6,2,4,6,4,4,4], target = 5 Output: "0" Explanation: It's not possible to paint any integer with total cost equal to target. Example 4: Input: cost = [6,10,15,40,40,40,40,40,40], target = 47 Output: "32211"   Constraints: cost.length == 9 1 <= cost[i] <= 5000 1 <= target <= 5000
class Solution: def largestNumber(self, cost: List[int], target: int) -> str: Memo = {} Min = min(cost) Memo[0] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] def dfs(remain): if remain in Memo: return Memo[remain] if remain < Min: Memo[remain] = [0, 0, 0, 0, 0, 0, 0, 0, 0, -10000000] return [0, 0, 0, 0, 0, 0, 0, 0, 0, -10000000] Max = [0, 0, 0, 0, 0, 0, 0, 0, 0, -10000000] flag = False for i, c in enumerate(cost): tmp = dfs(remain - c) if tmp[-1] != -10000000: flag = True newtmp = tmp[:i] + [tmp[i] + 1] + tmp[i + 1 : -1] + [tmp[-1] + 1] for j in range(9, -1, -1): if newtmp[j] > Max[j]: Max = newtmp break elif newtmp[j] == Max[j]: continue else: break Memo[remain] = Max return Max r = dfs(target) if r[-1] == -10000000: return "0" else: rs = "" for k in range(8, -1, -1): rs += str(k + 1) * r[k] return rs
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF IF VAR VAR RETURN VAR VAR IF VAR VAR ASSIGN VAR VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER RETURN LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR LIST BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER LIST BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER RETURN STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR
Given an array of integers cost and an integer target. Return the maximum integer you can paint under the following rules: The cost of painting a digit (i+1) is given by cost[i] (0 indexed). The total cost used must be equal to target. Integer does not have digits 0. Since the answer may be too large, return it as string. If there is no way to paint any integer given the condition, return "0".   Example 1: Input: cost = [4,3,2,5,6,7,2,5,5], target = 9 Output: "7772" Explanation: The cost to paint the digit '7' is 2, and the digit '2' is 3. Then cost("7772") = 2*3+ 3*1 = 9. You could also paint "977", but "7772" is the largest number. Digit cost 1 -> 4 2 -> 3 3 -> 2 4 -> 5 5 -> 6 6 -> 7 7 -> 2 8 -> 5 9 -> 5 Example 2: Input: cost = [7,6,5,5,5,6,8,7,8], target = 12 Output: "85" Explanation: The cost to paint the digit '8' is 7, and the digit '5' is 5. Then cost("85") = 7 + 5 = 12. Example 3: Input: cost = [2,4,6,2,4,6,4,4,4], target = 5 Output: "0" Explanation: It's not possible to paint any integer with total cost equal to target. Example 4: Input: cost = [6,10,15,40,40,40,40,40,40], target = 47 Output: "32211"   Constraints: cost.length == 9 1 <= cost[i] <= 5000 1 <= target <= 5000
class Solution: def largestNumber(self, cost: List[int], target: int) -> str: dp = [float("-inf")] * (target + 1) dp[0] = 0 for i in range(1, 10): for j in range(1, target + 1): c = cost[9 - i] if c <= j: dp[j] = max(dp[j - c] * 10 + (10 - i), dp[j]) return str(dp[target]) if str(dp[target]) != "-inf" else "0"
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP NUMBER VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR STRING FUNC_CALL VAR VAR VAR STRING VAR
Given an array of integers cost and an integer target. Return the maximum integer you can paint under the following rules: The cost of painting a digit (i+1) is given by cost[i] (0 indexed). The total cost used must be equal to target. Integer does not have digits 0. Since the answer may be too large, return it as string. If there is no way to paint any integer given the condition, return "0".   Example 1: Input: cost = [4,3,2,5,6,7,2,5,5], target = 9 Output: "7772" Explanation: The cost to paint the digit '7' is 2, and the digit '2' is 3. Then cost("7772") = 2*3+ 3*1 = 9. You could also paint "977", but "7772" is the largest number. Digit cost 1 -> 4 2 -> 3 3 -> 2 4 -> 5 5 -> 6 6 -> 7 7 -> 2 8 -> 5 9 -> 5 Example 2: Input: cost = [7,6,5,5,5,6,8,7,8], target = 12 Output: "85" Explanation: The cost to paint the digit '8' is 7, and the digit '5' is 5. Then cost("85") = 7 + 5 = 12. Example 3: Input: cost = [2,4,6,2,4,6,4,4,4], target = 5 Output: "0" Explanation: It's not possible to paint any integer with total cost equal to target. Example 4: Input: cost = [6,10,15,40,40,40,40,40,40], target = 47 Output: "32211"   Constraints: cost.length == 9 1 <= cost[i] <= 5000 1 <= target <= 5000
class Solution: def largestNumber(self, cost: List[int], target: int) -> str: dp = [0] * (target + 1) for t in range(1, target + 1): for d, c in zip(range(1, len(cost) + 1), cost): if t == c: dp[t] = max(dp[t], d) if c < t and dp[t - c] != 0: dp[t] = max(dp[t], dp[t - c] * 10 + d) return str(dp[target])
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR VAR
Given an array of integers cost and an integer target. Return the maximum integer you can paint under the following rules: The cost of painting a digit (i+1) is given by cost[i] (0 indexed). The total cost used must be equal to target. Integer does not have digits 0. Since the answer may be too large, return it as string. If there is no way to paint any integer given the condition, return "0".   Example 1: Input: cost = [4,3,2,5,6,7,2,5,5], target = 9 Output: "7772" Explanation: The cost to paint the digit '7' is 2, and the digit '2' is 3. Then cost("7772") = 2*3+ 3*1 = 9. You could also paint "977", but "7772" is the largest number. Digit cost 1 -> 4 2 -> 3 3 -> 2 4 -> 5 5 -> 6 6 -> 7 7 -> 2 8 -> 5 9 -> 5 Example 2: Input: cost = [7,6,5,5,5,6,8,7,8], target = 12 Output: "85" Explanation: The cost to paint the digit '8' is 7, and the digit '5' is 5. Then cost("85") = 7 + 5 = 12. Example 3: Input: cost = [2,4,6,2,4,6,4,4,4], target = 5 Output: "0" Explanation: It's not possible to paint any integer with total cost equal to target. Example 4: Input: cost = [6,10,15,40,40,40,40,40,40], target = 47 Output: "32211"   Constraints: cost.length == 9 1 <= cost[i] <= 5000 1 <= target <= 5000
class Solution: def largestNumber(self, cost: List[int], target: int) -> str: self.min_cost = min(cost) self.cost = cost self.mem = dict() rv = self.dfs(target) if rv is None: return "0" return rv def dfs(self, target): if target == 0: return "" elif target < self.min_cost: return None key = target if key in self.mem: return self.mem[key] res = "" for i, c in enumerate(self.cost): rv = self.dfs(target - c) if rv != None: res = self.get_max(res, str(i + 1) + rv) if res == "": res = None self.mem[key] = res return res def get_max(self, s1, s2): if len(s1) != len(s2): if len(s1) > len(s2): return s1 return s2 if s1 > s2: return s1 return s2
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NONE RETURN STRING RETURN VAR VAR FUNC_DEF IF VAR NUMBER RETURN STRING IF VAR VAR RETURN NONE ASSIGN VAR VAR IF VAR VAR RETURN VAR VAR ASSIGN VAR STRING FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR STRING ASSIGN VAR NONE ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR RETURN VAR IF VAR VAR RETURN VAR RETURN VAR
Given an array of integers cost and an integer target. Return the maximum integer you can paint under the following rules: The cost of painting a digit (i+1) is given by cost[i] (0 indexed). The total cost used must be equal to target. Integer does not have digits 0. Since the answer may be too large, return it as string. If there is no way to paint any integer given the condition, return "0".   Example 1: Input: cost = [4,3,2,5,6,7,2,5,5], target = 9 Output: "7772" Explanation: The cost to paint the digit '7' is 2, and the digit '2' is 3. Then cost("7772") = 2*3+ 3*1 = 9. You could also paint "977", but "7772" is the largest number. Digit cost 1 -> 4 2 -> 3 3 -> 2 4 -> 5 5 -> 6 6 -> 7 7 -> 2 8 -> 5 9 -> 5 Example 2: Input: cost = [7,6,5,5,5,6,8,7,8], target = 12 Output: "85" Explanation: The cost to paint the digit '8' is 7, and the digit '5' is 5. Then cost("85") = 7 + 5 = 12. Example 3: Input: cost = [2,4,6,2,4,6,4,4,4], target = 5 Output: "0" Explanation: It's not possible to paint any integer with total cost equal to target. Example 4: Input: cost = [6,10,15,40,40,40,40,40,40], target = 47 Output: "32211"   Constraints: cost.length == 9 1 <= cost[i] <= 5000 1 <= target <= 5000
class Solution: def largestNumber(self, cost: List[int], target: int) -> str: dp = [0] + [-target] * target for t in range(1, target + 1): dp[t] = max([dp[t - i] for i in cost if i <= t] + [dp[t]]) + 1 if dp[-1] <= 0: return "0" res = "" for i in range(8, -1, -1): while target >= cost[i] and dp[target - cost[i]] == dp[target] - 1: res += str(i + 1) target -= cost[i] return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR LIST VAR VAR NUMBER IF VAR NUMBER NUMBER RETURN STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER WHILE VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR VAR
Given an array of integers cost and an integer target. Return the maximum integer you can paint under the following rules: The cost of painting a digit (i+1) is given by cost[i] (0 indexed). The total cost used must be equal to target. Integer does not have digits 0. Since the answer may be too large, return it as string. If there is no way to paint any integer given the condition, return "0".   Example 1: Input: cost = [4,3,2,5,6,7,2,5,5], target = 9 Output: "7772" Explanation: The cost to paint the digit '7' is 2, and the digit '2' is 3. Then cost("7772") = 2*3+ 3*1 = 9. You could also paint "977", but "7772" is the largest number. Digit cost 1 -> 4 2 -> 3 3 -> 2 4 -> 5 5 -> 6 6 -> 7 7 -> 2 8 -> 5 9 -> 5 Example 2: Input: cost = [7,6,5,5,5,6,8,7,8], target = 12 Output: "85" Explanation: The cost to paint the digit '8' is 7, and the digit '5' is 5. Then cost("85") = 7 + 5 = 12. Example 3: Input: cost = [2,4,6,2,4,6,4,4,4], target = 5 Output: "0" Explanation: It's not possible to paint any integer with total cost equal to target. Example 4: Input: cost = [6,10,15,40,40,40,40,40,40], target = 47 Output: "32211"   Constraints: cost.length == 9 1 <= cost[i] <= 5000 1 <= target <= 5000
class Solution: def largestNumber(self, cost: List[int], target: int) -> str: return self.helper(cost, target, {}) def helper(self, cost, target, visited): if target == 0: return "" if target in visited: return visited[target] ans = "0" for i in range(1, 10): if target >= cost[i - 1]: prev = self.helper(cost, target - cost[i - 1], visited) if prev != "0": curr = str(i) + prev if len(curr) >= len(ans) and int(curr) > int(ans): ans = curr visited[target] = ans return ans
CLASS_DEF FUNC_DEF VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR DICT VAR FUNC_DEF IF VAR NUMBER RETURN STRING IF VAR VAR RETURN VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR IF VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR RETURN VAR
Given an array of integers cost and an integer target. Return the maximum integer you can paint under the following rules: The cost of painting a digit (i+1) is given by cost[i] (0 indexed). The total cost used must be equal to target. Integer does not have digits 0. Since the answer may be too large, return it as string. If there is no way to paint any integer given the condition, return "0".   Example 1: Input: cost = [4,3,2,5,6,7,2,5,5], target = 9 Output: "7772" Explanation: The cost to paint the digit '7' is 2, and the digit '2' is 3. Then cost("7772") = 2*3+ 3*1 = 9. You could also paint "977", but "7772" is the largest number. Digit cost 1 -> 4 2 -> 3 3 -> 2 4 -> 5 5 -> 6 6 -> 7 7 -> 2 8 -> 5 9 -> 5 Example 2: Input: cost = [7,6,5,5,5,6,8,7,8], target = 12 Output: "85" Explanation: The cost to paint the digit '8' is 7, and the digit '5' is 5. Then cost("85") = 7 + 5 = 12. Example 3: Input: cost = [2,4,6,2,4,6,4,4,4], target = 5 Output: "0" Explanation: It's not possible to paint any integer with total cost equal to target. Example 4: Input: cost = [6,10,15,40,40,40,40,40,40], target = 47 Output: "32211"   Constraints: cost.length == 9 1 <= cost[i] <= 5000 1 <= target <= 5000
def prune(costs): ans = [] for ind, elem in enumerate(costs): if any(costs[i] + costs[j] == elem for i in range(ind) for j in range(ind)): pass else: ans.append(elem) return ans def find_longest_combos(costs, start, prefix, left, max_yets): if start == len(costs): if left == 0 and len(prefix) >= max_yets[0]: max_yets[0] = len(prefix) yield prefix[:] else: theoretical_max_length = len(prefix) + left // costs[start] if theoretical_max_length < max_yets[0]: return if costs[start] <= left: prefix.append(costs[start]) yield from find_longest_combos( costs, start, prefix, left - costs[start], max_yets ) prefix.pop() if True: yield from find_longest_combos(costs, start + 1, prefix, left, max_yets) def rlencode(arr): ans = [] for elem in arr: if ans and ans[-1][0] == elem: ans[-1] = ans[-1][0], ans[-1][1] + 1 else: ans.append((elem, 1)) return ans def encode(digit_choices, combo): return sorted([(digit_choices[elem], freq) for elem, freq in combo], reverse=True) def write(rle): ans = [] for elem, freq in rle: for _ in range(freq): ans.append(elem) return "".join(map(str, ans)) def gcd(a, b): a, b = min(a, b), max(a, b) while a > 0: a, b = b % a, a return b def gcdall(arr): ans = arr[0] for elem in arr: ans = gcd(ans, elem) return ans def obviously_unsatisfactory(cost, target): if min(cost) > target: return True if target % gcdall(cost) != 0: return True return False def largest_num(cost, target): if obviously_unsatisfactory(cost, target): return "0" costs = sorted(list(set(cost))) costs = prune(costs) combinations = list(find_longest_combos(costs, 0, [], target, [0])) if not combinations: return "0" max_length = max(map(len, combinations)) combinations = (c for c in combinations if len(c) == max_length) digit_choices = {} for i, c in enumerate(cost): digit_choices[c] = i + 1 combinations = map(rlencode, combinations) combinations = (encode(digit_choices, combo) for combo in combinations) return write(max(combinations)) class Solution: def largestNumber(self, cost: List[int], target: int) -> str: return largest_num(cost, target)
FUNC_DEF ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR EXPR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR VAR NUMBER RETURN IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR IF NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR VAR IF VAR VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR LIST FOR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN NUMBER IF BIN_OP VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER LIST VAR LIST NUMBER IF VAR RETURN STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR
Given an array of integers cost and an integer target. Return the maximum integer you can paint under the following rules: The cost of painting a digit (i+1) is given by cost[i] (0 indexed). The total cost used must be equal to target. Integer does not have digits 0. Since the answer may be too large, return it as string. If there is no way to paint any integer given the condition, return "0".   Example 1: Input: cost = [4,3,2,5,6,7,2,5,5], target = 9 Output: "7772" Explanation: The cost to paint the digit '7' is 2, and the digit '2' is 3. Then cost("7772") = 2*3+ 3*1 = 9. You could also paint "977", but "7772" is the largest number. Digit cost 1 -> 4 2 -> 3 3 -> 2 4 -> 5 5 -> 6 6 -> 7 7 -> 2 8 -> 5 9 -> 5 Example 2: Input: cost = [7,6,5,5,5,6,8,7,8], target = 12 Output: "85" Explanation: The cost to paint the digit '8' is 7, and the digit '5' is 5. Then cost("85") = 7 + 5 = 12. Example 3: Input: cost = [2,4,6,2,4,6,4,4,4], target = 5 Output: "0" Explanation: It's not possible to paint any integer with total cost equal to target. Example 4: Input: cost = [6,10,15,40,40,40,40,40,40], target = 47 Output: "32211"   Constraints: cost.length == 9 1 <= cost[i] <= 5000 1 <= target <= 5000
class Solution: def largestNumber(self, cost: List[int], target: int) -> str: q = [(0, -target)] visited = {} costs = dict(zip(range(1, 10), cost)) for i, a in enumerate(cost, 1): for b in cost[i:]: if b <= a and not a % b: del costs[i] break while q: n, hp = heapq.heappop(q) n = -n hp = -hp if hp < 0: continue if visited.get(hp, -1) >= n: continue visited[hp] = n for c in costs: if costs[c] <= hp: heapq.heappush(q, (-(n * 10 + c), -(hp - costs[c]))) return str(visited.get(0, 0))
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST NUMBER VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR VAR FOR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER VAR
Given an array of integers cost and an integer target. Return the maximum integer you can paint under the following rules: The cost of painting a digit (i+1) is given by cost[i] (0 indexed). The total cost used must be equal to target. Integer does not have digits 0. Since the answer may be too large, return it as string. If there is no way to paint any integer given the condition, return "0".   Example 1: Input: cost = [4,3,2,5,6,7,2,5,5], target = 9 Output: "7772" Explanation: The cost to paint the digit '7' is 2, and the digit '2' is 3. Then cost("7772") = 2*3+ 3*1 = 9. You could also paint "977", but "7772" is the largest number. Digit cost 1 -> 4 2 -> 3 3 -> 2 4 -> 5 5 -> 6 6 -> 7 7 -> 2 8 -> 5 9 -> 5 Example 2: Input: cost = [7,6,5,5,5,6,8,7,8], target = 12 Output: "85" Explanation: The cost to paint the digit '8' is 7, and the digit '5' is 5. Then cost("85") = 7 + 5 = 12. Example 3: Input: cost = [2,4,6,2,4,6,4,4,4], target = 5 Output: "0" Explanation: It's not possible to paint any integer with total cost equal to target. Example 4: Input: cost = [6,10,15,40,40,40,40,40,40], target = 47 Output: "32211"   Constraints: cost.length == 9 1 <= cost[i] <= 5000 1 <= target <= 5000
class Solution: def largestNumber(self, cost: List[int], target: int) -> str: @lru_cache(None) def dfs(c): if c == 0: return 0 ans = -float("inf") for i in range(9): if cost[i] <= c: ans = max(ans, 10 * dfs(c - cost[i]) + i + 1) return ans res = dfs(target) return str(res) if res > 0 else "0"
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR FUNC_CALL VAR NONE ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR NUMBER FUNC_CALL VAR VAR STRING VAR
Given an array of integers cost and an integer target. Return the maximum integer you can paint under the following rules: The cost of painting a digit (i+1) is given by cost[i] (0 indexed). The total cost used must be equal to target. Integer does not have digits 0. Since the answer may be too large, return it as string. If there is no way to paint any integer given the condition, return "0".   Example 1: Input: cost = [4,3,2,5,6,7,2,5,5], target = 9 Output: "7772" Explanation: The cost to paint the digit '7' is 2, and the digit '2' is 3. Then cost("7772") = 2*3+ 3*1 = 9. You could also paint "977", but "7772" is the largest number. Digit cost 1 -> 4 2 -> 3 3 -> 2 4 -> 5 5 -> 6 6 -> 7 7 -> 2 8 -> 5 9 -> 5 Example 2: Input: cost = [7,6,5,5,5,6,8,7,8], target = 12 Output: "85" Explanation: The cost to paint the digit '8' is 7, and the digit '5' is 5. Then cost("85") = 7 + 5 = 12. Example 3: Input: cost = [2,4,6,2,4,6,4,4,4], target = 5 Output: "0" Explanation: It's not possible to paint any integer with total cost equal to target. Example 4: Input: cost = [6,10,15,40,40,40,40,40,40], target = 47 Output: "32211"   Constraints: cost.length == 9 1 <= cost[i] <= 5000 1 <= target <= 5000
class Solution: def largestNumber(self, cost: List[int], target: int) -> str: @lru_cache(None) def rec(rem): if rem <= 0: return 0 if rem == 0 else -math.inf ans = -math.inf for c in cost: ans = max(ans, rec(rem - c) + 1) return ans ln = rec(target) if ln <= 0: return "0" def path(rem, now): if rem <= 0: return now if rem == 0 else "" for i, c in enumerate(reversed(cost)): if rec(rem - c) + 1 == rec(rem): return path(rem - c, now + str(9 - i)) return "" ans = path(target, "") print(ans) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER NUMBER VAR ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER RETURN VAR FUNC_CALL VAR NONE ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN STRING FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER VAR STRING FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP NUMBER VAR RETURN STRING ASSIGN VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given an array of integers cost and an integer target. Return the maximum integer you can paint under the following rules: The cost of painting a digit (i+1) is given by cost[i] (0 indexed). The total cost used must be equal to target. Integer does not have digits 0. Since the answer may be too large, return it as string. If there is no way to paint any integer given the condition, return "0".   Example 1: Input: cost = [4,3,2,5,6,7,2,5,5], target = 9 Output: "7772" Explanation: The cost to paint the digit '7' is 2, and the digit '2' is 3. Then cost("7772") = 2*3+ 3*1 = 9. You could also paint "977", but "7772" is the largest number. Digit cost 1 -> 4 2 -> 3 3 -> 2 4 -> 5 5 -> 6 6 -> 7 7 -> 2 8 -> 5 9 -> 5 Example 2: Input: cost = [7,6,5,5,5,6,8,7,8], target = 12 Output: "85" Explanation: The cost to paint the digit '8' is 7, and the digit '5' is 5. Then cost("85") = 7 + 5 = 12. Example 3: Input: cost = [2,4,6,2,4,6,4,4,4], target = 5 Output: "0" Explanation: It's not possible to paint any integer with total cost equal to target. Example 4: Input: cost = [6,10,15,40,40,40,40,40,40], target = 47 Output: "32211"   Constraints: cost.length == 9 1 <= cost[i] <= 5000 1 <= target <= 5000
class Solution: def largestNumber(self, cost: List[int], target: int) -> str: dp = [0] + [-float("inf")] * (5000 + target) for i in range(1, target + 1): for j, c in enumerate(cost): dp[i] = max(dp[i], 10 * dp[i - c] + j + 1) print(dp[: target + 1]) return str(max(dp[target], 0))
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST FUNC_CALL VAR STRING BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR
Given an array of integers cost and an integer target. Return the maximum integer you can paint under the following rules: The cost of painting a digit (i+1) is given by cost[i] (0 indexed). The total cost used must be equal to target. Integer does not have digits 0. Since the answer may be too large, return it as string. If there is no way to paint any integer given the condition, return "0".   Example 1: Input: cost = [4,3,2,5,6,7,2,5,5], target = 9 Output: "7772" Explanation: The cost to paint the digit '7' is 2, and the digit '2' is 3. Then cost("7772") = 2*3+ 3*1 = 9. You could also paint "977", but "7772" is the largest number. Digit cost 1 -> 4 2 -> 3 3 -> 2 4 -> 5 5 -> 6 6 -> 7 7 -> 2 8 -> 5 9 -> 5 Example 2: Input: cost = [7,6,5,5,5,6,8,7,8], target = 12 Output: "85" Explanation: The cost to paint the digit '8' is 7, and the digit '5' is 5. Then cost("85") = 7 + 5 = 12. Example 3: Input: cost = [2,4,6,2,4,6,4,4,4], target = 5 Output: "0" Explanation: It's not possible to paint any integer with total cost equal to target. Example 4: Input: cost = [6,10,15,40,40,40,40,40,40], target = 47 Output: "32211"   Constraints: cost.length == 9 1 <= cost[i] <= 5000 1 <= target <= 5000
class Solution: def largestNumber(self, cost: List[int], target: int) -> str: t = target dp = [["0"] for _ in range(t + 1)] dp[0] = [""] for i in range(1, t + 1): for j in range(9): l1 = len(dp[i]) l1 = 0 if dp[i] == ["0"] else l1 l2 = len(dp[i - cost[j]]) + 1 if i >= cost[j] else 0 c = dp[i - cost[j]] if i >= cost[j] else ["0"] if l1 <= l2: dp[i] = c + [str(j + 1)] if c != ["0"] else ["0"] ans = "".join(sorted(dp[t])[::-1]) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST STRING VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER LIST STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR LIST STRING NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR LIST STRING IF VAR VAR ASSIGN VAR VAR VAR LIST STRING BIN_OP VAR LIST FUNC_CALL VAR BIN_OP VAR NUMBER LIST STRING ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR
Given an array of integers cost and an integer target. Return the maximum integer you can paint under the following rules: The cost of painting a digit (i+1) is given by cost[i] (0 indexed). The total cost used must be equal to target. Integer does not have digits 0. Since the answer may be too large, return it as string. If there is no way to paint any integer given the condition, return "0".   Example 1: Input: cost = [4,3,2,5,6,7,2,5,5], target = 9 Output: "7772" Explanation: The cost to paint the digit '7' is 2, and the digit '2' is 3. Then cost("7772") = 2*3+ 3*1 = 9. You could also paint "977", but "7772" is the largest number. Digit cost 1 -> 4 2 -> 3 3 -> 2 4 -> 5 5 -> 6 6 -> 7 7 -> 2 8 -> 5 9 -> 5 Example 2: Input: cost = [7,6,5,5,5,6,8,7,8], target = 12 Output: "85" Explanation: The cost to paint the digit '8' is 7, and the digit '5' is 5. Then cost("85") = 7 + 5 = 12. Example 3: Input: cost = [2,4,6,2,4,6,4,4,4], target = 5 Output: "0" Explanation: It's not possible to paint any integer with total cost equal to target. Example 4: Input: cost = [6,10,15,40,40,40,40,40,40], target = 47 Output: "32211"   Constraints: cost.length == 9 1 <= cost[i] <= 5000 1 <= target <= 5000
class Solution: def largestNumber(self, cost: List[int], target: int) -> str: def memo(f): dic = {} def f_alt(*args): if args not in dic: dic[args] = f(*args) return dic[args] return f_alt key = lambda s: (len(s), s) cands = {} for i, c in enumerate(cost): cands[c] = i + 1 @memo def f(tar): if tar == 0: return 0 if tar > 0: ans = 0 for c in cands: if f(tar - c) != None: ans = max(ans, 10 * f(tar - c) + cands[c]) return ans or None return str(f(target) or 0)
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR NONE ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN VAR NONE VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR
Given an array of integers cost and an integer target. Return the maximum integer you can paint under the following rules: The cost of painting a digit (i+1) is given by cost[i] (0 indexed). The total cost used must be equal to target. Integer does not have digits 0. Since the answer may be too large, return it as string. If there is no way to paint any integer given the condition, return "0".   Example 1: Input: cost = [4,3,2,5,6,7,2,5,5], target = 9 Output: "7772" Explanation: The cost to paint the digit '7' is 2, and the digit '2' is 3. Then cost("7772") = 2*3+ 3*1 = 9. You could also paint "977", but "7772" is the largest number. Digit cost 1 -> 4 2 -> 3 3 -> 2 4 -> 5 5 -> 6 6 -> 7 7 -> 2 8 -> 5 9 -> 5 Example 2: Input: cost = [7,6,5,5,5,6,8,7,8], target = 12 Output: "85" Explanation: The cost to paint the digit '8' is 7, and the digit '5' is 5. Then cost("85") = 7 + 5 = 12. Example 3: Input: cost = [2,4,6,2,4,6,4,4,4], target = 5 Output: "0" Explanation: It's not possible to paint any integer with total cost equal to target. Example 4: Input: cost = [6,10,15,40,40,40,40,40,40], target = 47 Output: "32211"   Constraints: cost.length == 9 1 <= cost[i] <= 5000 1 <= target <= 5000
def dp(costs, target, mem): if target == 0: return "" elif target in mem: return mem[target] res = "" for i in range(len(costs)): if target - costs[i] >= 0: current = dp(costs, target - costs[i], mem) if current is not None: res = max([res, str(i + 1) + current], key=lambda x: (len(x), x)) mem[target] = res if res != "" else None return res if res != "" else None class Solution: def largestNumber(self, cost: List[int], target: int) -> str: result = dp(cost, target, {}) return result if result != "" and result is not None else "0"
FUNC_DEF IF VAR NUMBER RETURN STRING IF VAR VAR RETURN VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR IF VAR NONE ASSIGN VAR FUNC_CALL VAR LIST VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR STRING VAR NONE RETURN VAR STRING VAR NONE CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR DICT RETURN VAR STRING VAR NONE VAR STRING VAR
Given a fence with n posts and k colors, find out the number of ways of painting the fence so that not more than two consecutive posts have the same colors. Since the answer can be large return it modulo 10^9 + 7. Example 1: Input: N=3, K=2 Output: 6 Explanation: We have following possible combinations: Example 2: Input: N=2, K=4 Output: 16 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countWays() that takes n and k as parameters and returns the number of ways in which the fence can be painted.(modulo 10^{9} + 7) Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 5000 1 ≤ K ≤ 100
import sys sys.setrecursionlimit(10000) sys.setrecursionlimit(10000) class Solution: def getAns(self, n, col, len, dp, colors): m = 10**9 + 7 if n == 0: return 1 if dp[n][col][len] != -1: return dp[n][col][len] notTake = 0 for i in range(0, colors): if i != col: notTake += self.getAns(n - 1, i, 1, dp, colors) take = 0 if len < 2: take = self.getAns(n - 1, col, len + 1, dp, colors) dp[n][col][len] = (notTake + take) % m return dp[n][col][len] def countWays(self, n, k): m = 10**9 + 7 dp = [(0) for i in range(n + 1)] dp[1] = k same = k for i in range(2, n + 1): dp[i] = (same + dp[i - 1] * (k - 1)) % m same = dp[i - 1] * (k - 1) % m return dp[n] dp = [[[(-1) for i in range(3)] for j in range(k)] for jj in range(n + 1)] val = self.getAns(n, k - 1, 0, dp, k) return val
IMPORT EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR RETURN VAR
Given a fence with n posts and k colors, find out the number of ways of painting the fence so that not more than two consecutive posts have the same colors. Since the answer can be large return it modulo 10^9 + 7. Example 1: Input: N=3, K=2 Output: 6 Explanation: We have following possible combinations: Example 2: Input: N=2, K=4 Output: 16 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countWays() that takes n and k as parameters and returns the number of ways in which the fence can be painted.(modulo 10^{9} + 7) Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 5000 1 ≤ K ≤ 100
class Solution: def countWays(self, n, k): if n == 0: return 0 if n == 1: return k if n == 2: return k * k res = [0] * (n + 1) res[1] = k res[2] = k * k for i in range(3, n + 1): res[i] = (k - 1) * (res[i - 1] + res[i - 2]) % (10**9 + 7) return res[n]
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR VAR
Given a fence with n posts and k colors, find out the number of ways of painting the fence so that not more than two consecutive posts have the same colors. Since the answer can be large return it modulo 10^9 + 7. Example 1: Input: N=3, K=2 Output: 6 Explanation: We have following possible combinations: Example 2: Input: N=2, K=4 Output: 16 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countWays() that takes n and k as parameters and returns the number of ways in which the fence can be painted.(modulo 10^{9} + 7) Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 5000 1 ≤ K ≤ 100
class Solution: def countWays(self, n, k): mod = 1000000007 if n == 0: return 0 if n == 1: return k same = k % mod diff = k * (k - 1) % mod for i in range(3, n + 1): prev = diff % mod diff = (same + diff) * (k - 1) % mod same = prev % mod return (same + diff) % mod
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR
Given a fence with n posts and k colors, find out the number of ways of painting the fence so that not more than two consecutive posts have the same colors. Since the answer can be large return it modulo 10^9 + 7. Example 1: Input: N=3, K=2 Output: 6 Explanation: We have following possible combinations: Example 2: Input: N=2, K=4 Output: 16 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countWays() that takes n and k as parameters and returns the number of ways in which the fence can be painted.(modulo 10^{9} + 7) Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 5000 1 ≤ K ≤ 100
import sys sys.setrecursionlimit(10**9) mod = 10**9 + 7 def add(a, b): return (a % mod + b % mod) % mod def mul(a, b): return a % mod * (b % mod) % mod def solve(n, k, dp): if n == 1: return k if n == 2: return add(k, mul(k, k - 1)) if dp[n][k] != -1: return dp[n][k] inc1 = mul(solve(n - 2, k, dp), k - 1) inc2 = mul(solve(n - 1, k, dp), k - 1) summ = add(inc1, inc2) dp[n][k] = summ return dp[n][k] class Solution: def countWays(self, n, k): if n == 1: return k if n == 2: return k**2 % mod dp = [[(-1) for _ in range(k + 1)] for _ in range(n + 1)] return solve(n, k, dp)
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR
Given a fence with n posts and k colors, find out the number of ways of painting the fence so that not more than two consecutive posts have the same colors. Since the answer can be large return it modulo 10^9 + 7. Example 1: Input: N=3, K=2 Output: 6 Explanation: We have following possible combinations: Example 2: Input: N=2, K=4 Output: 16 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countWays() that takes n and k as parameters and returns the number of ways in which the fence can be painted.(modulo 10^{9} + 7) Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 5000 1 ≤ K ≤ 100
import sys sys.setrecursionlimit(10000000) class Solution: def solve(self, n, k, dp): if n == 1: return k if n == 2: return k + k * (k - 1) if dp[n] != -1: return dp[n] a = self.solve(n - 2, k, dp) ans_a = a * (k - 1) b = self.solve(n - 1, k, dp) ans_b = b * (k - 1) ans = ans_a + ans_b dp[n] = ans return dp[n] def countWays(self, n, k): dp = [(-1) for i in range(n + 1)] ans = self.solve(n, k, dp) return ans % 1000000007
IMPORT EXPR FUNC_CALL VAR NUMBER CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN BIN_OP VAR NUMBER
Given a fence with n posts and k colors, find out the number of ways of painting the fence so that not more than two consecutive posts have the same colors. Since the answer can be large return it modulo 10^9 + 7. Example 1: Input: N=3, K=2 Output: 6 Explanation: We have following possible combinations: Example 2: Input: N=2, K=4 Output: 16 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countWays() that takes n and k as parameters and returns the number of ways in which the fence can be painted.(modulo 10^{9} + 7) Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 5000 1 ≤ K ≤ 100
import sys sys.setrecursionlimit(10**9) class Solution: def solve(self, n, k, dp, mod): if n <= 0: return 0 if n == 1: return k % mod if n == 2: return k**2 % mod if dp[n][k] != -1: return dp[n][k] summ = (self.solve(n - 1, k, dp, mod) + self.solve(n - 2, k, dp, mod)) % mod dp[n][k] = (k - 1) * summ % mod return dp[n][k] def countWays(self, n, k): mod = 10**9 + 7 if n == 1: return k % mod if n == 2: return k**2 % mod dp = [[(-1) for _ in range(k + 1)] for _ in range(n + 1)] ans = self.solve(n, k, dp, mod) return ans
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN BIN_OP VAR VAR IF VAR NUMBER RETURN BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR NUMBER RETURN BIN_OP VAR VAR IF VAR NUMBER RETURN BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR
Given a fence with n posts and k colors, find out the number of ways of painting the fence so that not more than two consecutive posts have the same colors. Since the answer can be large return it modulo 10^9 + 7. Example 1: Input: N=3, K=2 Output: 6 Explanation: We have following possible combinations: Example 2: Input: N=2, K=4 Output: 16 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countWays() that takes n and k as parameters and returns the number of ways in which the fence can be painted.(modulo 10^{9} + 7) Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 5000 1 ≤ K ≤ 100
class Solution: def countWays(self, n, k): if n == 1: return k limit = 1000000007 a, b = 1, k for i in range(3, n + 1): a, b = b, (k - 1) * (a + b) % limit return k * b % limit
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR
Given a fence with n posts and k colors, find out the number of ways of painting the fence so that not more than two consecutive posts have the same colors. Since the answer can be large return it modulo 10^9 + 7. Example 1: Input: N=3, K=2 Output: 6 Explanation: We have following possible combinations: Example 2: Input: N=2, K=4 Output: 16 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countWays() that takes n and k as parameters and returns the number of ways in which the fence can be painted.(modulo 10^{9} + 7) Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 5000 1 ≤ K ≤ 100
class Solution: def countWays(self, n, k): f = [] f.append(0) if n == 0: return f[0] f.append(k) if n == 1: return f[1] f.append(k * k) if n == 2: return f[2] for i in range(3, n + 1): f.append(f[i - 1] * (k - 1) + f[i - 2] * (k - 1)) return f[n] % 1000000007
CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER RETURN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER RETURN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR NUMBER
Given a fence with n posts and k colors, find out the number of ways of painting the fence so that not more than two consecutive posts have the same colors. Since the answer can be large return it modulo 10^9 + 7. Example 1: Input: N=3, K=2 Output: 6 Explanation: We have following possible combinations: Example 2: Input: N=2, K=4 Output: 16 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countWays() that takes n and k as parameters and returns the number of ways in which the fence can be painted.(modulo 10^{9} + 7) Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 5000 1 ≤ K ≤ 100
import sys class Solution: def countWays(self, n, k): sys.setrecursionlimit(6000) dp = [-1] * (1 + n) def findsol(n, k): if n == 1: return k if n == 2: return k * (k - 1) + k if n <= 0: return 0 if dp[n] != -1: return dp[n] ans = findsol(n - 1, k) % 1000000007 ans2 = findsol(n - 2, k) % 1000000007 ans = (ans + ans2) % 1000000007 ans = ans * (k - 1) % 1000000007 dp[n] = ans return ans return findsol(n, k)
IMPORT CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR FUNC_DEF IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR
Given a fence with n posts and k colors, find out the number of ways of painting the fence so that not more than two consecutive posts have the same colors. Since the answer can be large return it modulo 10^9 + 7. Example 1: Input: N=3, K=2 Output: 6 Explanation: We have following possible combinations: Example 2: Input: N=2, K=4 Output: 16 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countWays() that takes n and k as parameters and returns the number of ways in which the fence can be painted.(modulo 10^{9} + 7) Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 5000 1 ≤ K ≤ 100
class Solution: def countWays(self, n, k): MOD = 10**9 + 7 dp = [[(0) for _ in range(2)] for _ in range(n + 1)] dp[1][0] = k dp[1][1] = 0 for i in range(2, n + 1): dp[i][0] = (dp[i - 1][0] + dp[i - 1][1]) * (k - 1) dp[i][1] = dp[i - 1][0] dp[i][0] %= MOD dp[i][1] %= MOD return (dp[n][0] + dp[n][1]) % MOD
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR RETURN BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR
Given a fence with n posts and k colors, find out the number of ways of painting the fence so that not more than two consecutive posts have the same colors. Since the answer can be large return it modulo 10^9 + 7. Example 1: Input: N=3, K=2 Output: 6 Explanation: We have following possible combinations: Example 2: Input: N=2, K=4 Output: 16 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countWays() that takes n and k as parameters and returns the number of ways in which the fence can be painted.(modulo 10^{9} + 7) Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 5000 1 ≤ K ≤ 100
class Solution: def countWays(self, n, k): if n == 1: return k if n == 2: return k + k * (k - 1) mod = 10**9 + 7 ans = self.solve(n, k) return ans % mod def solve(self, n, k): mod = 10**9 + 7 dp = [0] * (n + 1) dp[1] = k dp[2] = k + k * (k - 1) for i in range(3, n + 1): dp[i] = (dp[i - 2] * (k - 1) + dp[i - 1] * (k - 1)) % mod return dp[n]
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN VAR VAR
Given a fence with n posts and k colors, find out the number of ways of painting the fence so that not more than two consecutive posts have the same colors. Since the answer can be large return it modulo 10^9 + 7. Example 1: Input: N=3, K=2 Output: 6 Explanation: We have following possible combinations: Example 2: Input: N=2, K=4 Output: 16 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countWays() that takes n and k as parameters and returns the number of ways in which the fence can be painted.(modulo 10^{9} + 7) Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 5000 1 ≤ K ≤ 100
class Solution: def rec_sol(self, n, N, k): if k == 0: return 0 if n == N: if n == 1: return k if n == 2: return k + k * (k - 1) return self.rec_sol(n - 1, N, k) * k + self.rec_sol(n - 2, N, k) * k else: if n == 1: return k - 1 if n == 2: return k - 1 + (k - 1) * (k - 1) return self.rec_sol(n - 1, N, k) * k + self.rec_sol(n - 2, N, k) * k return self.rec_sol(n - 1, N, k) * (k - 1) + self.rec_sol(n - 2, N, k) * (k - 1) def dp_sol(self, n, k): if k == 0: return 0 if n == 1: return k if n == 2: return k + k * (k - 1) else: suma1 = k - 1 suma2 = k * k - k for i in range(3, n + 1): if i == n: temp = suma2 * k + suma1 * k else: temp = suma2 * (k - 1) + suma1 * (k - 1) suma1 = suma2 suma2 = temp return temp % 1000000007 def countWays(self, n, k): return self.dp_sol(n, k)
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER RETURN BIN_OP VAR NUMBER IF VAR NUMBER RETURN BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR RETURN BIN_OP VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR VAR
Given a fence with n posts and k colors, find out the number of ways of painting the fence so that not more than two consecutive posts have the same colors. Since the answer can be large return it modulo 10^9 + 7. Example 1: Input: N=3, K=2 Output: 6 Explanation: We have following possible combinations: Example 2: Input: N=2, K=4 Output: 16 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countWays() that takes n and k as parameters and returns the number of ways in which the fence can be painted.(modulo 10^{9} + 7) Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 5000 1 ≤ K ≤ 100
class Solution: def countWays(self, n, k): if n == 1: return k if n == 2: return k + k * (k - 1) mod = 10**9 + 7 ans = self.solve(n, k) return ans % mod def solve(self, n, k): mod = 10**9 + 7 prev2 = k prev1 = k + k * (k - 1) for i in range(3, n + 1): ans = (prev2 * (k - 1) + prev1 * (k - 1)) % mod prev2 = prev1 prev1 = ans return prev1
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Given a fence with n posts and k colors, find out the number of ways of painting the fence so that not more than two consecutive posts have the same colors. Since the answer can be large return it modulo 10^9 + 7. Example 1: Input: N=3, K=2 Output: 6 Explanation: We have following possible combinations: Example 2: Input: N=2, K=4 Output: 16 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countWays() that takes n and k as parameters and returns the number of ways in which the fence can be painted.(modulo 10^{9} + 7) Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 5000 1 ≤ K ≤ 100
class Solution: def countWays(self, n, k): mod = 10**9 + 7 if n == 1: return k same = k diff = k * (k - 1) total = same + diff for i in range(3, n + 1): same = diff % mod diff = total * (k - 1) % mod total = (same + diff) % mod return total
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR
Given a fence with n posts and k colors, find out the number of ways of painting the fence so that not more than two consecutive posts have the same colors. Since the answer can be large return it modulo 10^9 + 7. Example 1: Input: N=3, K=2 Output: 6 Explanation: We have following possible combinations: Example 2: Input: N=2, K=4 Output: 16 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countWays() that takes n and k as parameters and returns the number of ways in which the fence can be painted.(modulo 10^{9} + 7) Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 5000 1 ≤ K ≤ 100
class Solution: def countWays(self, n, k): if n == 1: return k if n == 2: return k**2 same, different = k, k**2 - k for _ in range(2, n): newSame = different newDifferent = (same + different) * (k - 1) same, different = newSame, newDifferent return (same + different) % (10**9 + 7)
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER
Given a fence with n posts and k colors, find out the number of ways of painting the fence so that not more than two consecutive posts have the same colors. Since the answer can be large return it modulo 10^9 + 7. Example 1: Input: N=3, K=2 Output: 6 Explanation: We have following possible combinations: Example 2: Input: N=2, K=4 Output: 16 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countWays() that takes n and k as parameters and returns the number of ways in which the fence can be painted.(modulo 10^{9} + 7) Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 5000 1 ≤ K ≤ 100
import sys class Solution: def countWays(self, n, k): if n <= 0: return 0 if n == 1: return k dp = [-1] * (1 + n) dp[0] = 0 dp[1] = k dp[2] = k * (k - 1) + k for i in range(3, n + 1): ans = 0 ans2 = 0 if i - 1 >= 0: ans = dp[i - 1] % 1000000007 if i - 2 >= 0: ans2 = dp[i - 2] % 1000000007 ans = (ans + ans2) % 1000000007 ans = ans * (k - 1) % 1000000007 dp[i] = ans return dp[n]
IMPORT CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR
Given a fence with n posts and k colors, find out the number of ways of painting the fence so that not more than two consecutive posts have the same colors. Since the answer can be large return it modulo 10^9 + 7. Example 1: Input: N=3, K=2 Output: 6 Explanation: We have following possible combinations: Example 2: Input: N=2, K=4 Output: 16 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countWays() that takes n and k as parameters and returns the number of ways in which the fence can be painted.(modulo 10^{9} + 7) Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 5000 1 ≤ K ≤ 100
class Solution: def countWays(self, n, k): dp = [] for i in range(n): dp.append([0, 0]) dp[0] = [k, 0] for i in range(1, n): dp[i][0] = (dp[i - 1][0] * (k - 1) + dp[i - 1][1] * (k - 1)) % (10**9 + 7) dp[i][1] = dp[i - 1][0] return sum(dp[n - 1]) % (10**9 + 7)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER
There is a rooted tree of N vertices rooted at vertex 1. Each vertex v has a value A_{v} associated with it. You choose a vertex v (possibly the root) from the tree and remove all vertices on the path from the root to the vertex v, also including v. This will result in a forest of zero or more connected components. The beauty of a connected component is the \mathrm{GCD} of the values of all vertices in the component. Find the maximum value of the sum of beauties of the obtained connected components for any choice of v. Here, \mathrm{GCD} stands for [Greatest Common Divisor]. ------ Input Format ------ - The first line contains a single integer T — the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N — the size of the tree. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the values associated with each vertex. - The next N-1 lines contain two space-separated integers u and v — denoting an undirected edge between nodes u and v. It is guaranteed that the edges given in the input form a tree. ------ Output Format ------ For each test case output the maximum value of the sum of beauties of the obtained connected components for any choice of v. ------ Constraints ------ $1 ≤ T ≤ 2\cdot10^{4}$ $1 ≤ N ≤ 3\cdot10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ $1 ≤ u,v ≤ N$ and $u \neq v$ - It is guaranteed that the edges given in the input form a tree. - The sum of $N$ over all test cases does not exceed $3\cdot10^{5}$ ----- Sample Input 1 ------ 1 10 15 30 15 5 3 15 3 3 5 5 1 2 1 5 2 3 2 4 5 6 5 7 5 8 7 9 7 10 ----- Sample Output 1 ------ 33 ----- explanation 1 ------ The tree from the sample is as follows. ![tree_{basic}] If vertex $v = 7$ is chosen, vertices $1$, $5$ and $7$ are removed. ![tree_{remove}] The resulting forest contains five connected components $\{8\}, \{6\}, \{10\}, \{9\}$ and $\{2, 3, 4\}$. ![tree_{value}] The beauties of the connected components are $3$, $15$, $5$, $5$ and $5$ respectively. Thus the answer is $3 + 15 + 5 + 5 + 5 = 33$. It can be shown that this is the maximum value possible for any choice of $v$.
import sys class Node: def __init__(self, val): self.val = val self.gcd = val self.sumOfGcdChildren = 0 def __str__(self): return f" {self.gcd} {self.sumOfGcdChildren} {self.val}" def __gcd(a, b): if b == 0: return a return __gcd(b, a % b) ans = 0 def run(): n = int(input()) nodeval = list(map(int, input().split())) nodes = {} for i in range(1, n + 1): nodes[i] = Node(nodeval[i - 1]) graph = {} for i in range(1, n + 1): graph[i] = [] for i in range(n - 1): a, b = map(int, input().split()) graph[a].append(b) graph[b].append(a) def dfs(curr, parent): sumofgcd = 0 for child in graph[curr]: if child != parent: dfs(child, curr) nodes[curr].gcd = __gcd(nodes[curr].gcd, nodes[child].gcd) sumofgcd += nodes[child].gcd nodes[curr].sumOfGcdChildren = sumofgcd dfs(1, -1) global ans ans = 0 def dfs2(curr, parent, sum): sum += nodes[curr].sumOfGcdChildren global ans ans = max(ans, sum) for child in graph[curr]: if child != parent: dfs2(child, curr, sum - nodes[child].gcd) dfs2(1, -1, 0) print(ans) sys.setrecursionlimit(10**6) test = int(input()) for i in range(test): run()
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FUNC_DEF RETURN STRING VAR STRING VAR STRING VAR FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
There is a rooted tree of N vertices rooted at vertex 1. Each vertex v has a value A_{v} associated with it. You choose a vertex v (possibly the root) from the tree and remove all vertices on the path from the root to the vertex v, also including v. This will result in a forest of zero or more connected components. The beauty of a connected component is the \mathrm{GCD} of the values of all vertices in the component. Find the maximum value of the sum of beauties of the obtained connected components for any choice of v. Here, \mathrm{GCD} stands for [Greatest Common Divisor]. ------ Input Format ------ - The first line contains a single integer T — the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N — the size of the tree. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the values associated with each vertex. - The next N-1 lines contain two space-separated integers u and v — denoting an undirected edge between nodes u and v. It is guaranteed that the edges given in the input form a tree. ------ Output Format ------ For each test case output the maximum value of the sum of beauties of the obtained connected components for any choice of v. ------ Constraints ------ $1 ≤ T ≤ 2\cdot10^{4}$ $1 ≤ N ≤ 3\cdot10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ $1 ≤ u,v ≤ N$ and $u \neq v$ - It is guaranteed that the edges given in the input form a tree. - The sum of $N$ over all test cases does not exceed $3\cdot10^{5}$ ----- Sample Input 1 ------ 1 10 15 30 15 5 3 15 3 3 5 5 1 2 1 5 2 3 2 4 5 6 5 7 5 8 7 9 7 10 ----- Sample Output 1 ------ 33 ----- explanation 1 ------ The tree from the sample is as follows. ![tree_{basic}] If vertex $v = 7$ is chosen, vertices $1$, $5$ and $7$ are removed. ![tree_{remove}] The resulting forest contains five connected components $\{8\}, \{6\}, \{10\}, \{9\}$ and $\{2, 3, 4\}$. ![tree_{value}] The beauties of the connected components are $3$, $15$, $5$, $5$ and $5$ respectively. Thus the answer is $3 + 15 + 5 + 5 + 5 = 33$. It can be shown that this is the maximum value possible for any choice of $v$.
from sys import setrecursionlimit, stdin input = stdin.readline setrecursionlimit(4 * 10**5) inp = lambda: list(map(int, input().split())) def gcd(a, b): if b == 0: return a return gcd(b, a % b) def solve(p, prev, take): if dp[p][take] != -1: return dp[p][take] if take == 0: gcdval = a[p - 1] for i in child[p]: if i == prev: continue gcdval = gcd(gcdval, solve(i, p, 0)) dp[p][take] = gcdval return gcdval else: total = 0 for i in child[p]: if i == prev: continue total += solve(i, p, 0) bestans = total for i in child[p]: if i == prev: continue curans = total - solve(i, p, 0) + solve(i, p, 1) bestans = max(bestans, curans) dp[p][take] = bestans return bestans for T in range(int(input())): n = int(input()) a = inp() child = [[] for i in range(n + 1)] for i in range(n - 1): u, v = inp() child[u].append(v) child[v].append(u) dp = [[-1, -1] for i in range(n + 1)] ans = max(solve(1, -1, 0), solve(1, -1, 1)) print(ans)
ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_DEF IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER NUMBER FUNC_CALL VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR
There is a rooted tree of N vertices rooted at vertex 1. Each vertex v has a value A_{v} associated with it. You choose a vertex v (possibly the root) from the tree and remove all vertices on the path from the root to the vertex v, also including v. This will result in a forest of zero or more connected components. The beauty of a connected component is the \mathrm{GCD} of the values of all vertices in the component. Find the maximum value of the sum of beauties of the obtained connected components for any choice of v. Here, \mathrm{GCD} stands for [Greatest Common Divisor]. ------ Input Format ------ - The first line contains a single integer T — the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N — the size of the tree. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the values associated with each vertex. - The next N-1 lines contain two space-separated integers u and v — denoting an undirected edge between nodes u and v. It is guaranteed that the edges given in the input form a tree. ------ Output Format ------ For each test case output the maximum value of the sum of beauties of the obtained connected components for any choice of v. ------ Constraints ------ $1 ≤ T ≤ 2\cdot10^{4}$ $1 ≤ N ≤ 3\cdot10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ $1 ≤ u,v ≤ N$ and $u \neq v$ - It is guaranteed that the edges given in the input form a tree. - The sum of $N$ over all test cases does not exceed $3\cdot10^{5}$ ----- Sample Input 1 ------ 1 10 15 30 15 5 3 15 3 3 5 5 1 2 1 5 2 3 2 4 5 6 5 7 5 8 7 9 7 10 ----- Sample Output 1 ------ 33 ----- explanation 1 ------ The tree from the sample is as follows. ![tree_{basic}] If vertex $v = 7$ is chosen, vertices $1$, $5$ and $7$ are removed. ![tree_{remove}] The resulting forest contains five connected components $\{8\}, \{6\}, \{10\}, \{9\}$ and $\{2, 3, 4\}$. ![tree_{value}] The beauties of the connected components are $3$, $15$, $5$, $5$ and $5$ respectively. Thus the answer is $3 + 15 + 5 + 5 + 5 = 33$. It can be shown that this is the maximum value possible for any choice of $v$.
def gcd(x, y): if y > x: x, y = y, x if y == 0: return x if x == 0: return y x = x % y return gcd(y, x) class Node: parent = None def __init__(self, value, index): self.value = value self.index = index self.child = [] self.tree_gcd = None self.beauty_ = None def gcd_of_tree(self): if self.tree_gcd != None: return self.tree_gcd x = self.value for c in self.child: x = gcd(x, c.gcd_of_tree()) self.tree_gcd = x return x def beauty(self): if self.beauty_ != None: return self.beauty_ if self.parent == None: sum_ = 0 else: sum_ = self.parent.beauty() sum_ = sum_ - self.gcd_of_tree() for c in self.child: sum_ = sum_ + c.gcd_of_tree() self.beauty_ = sum_ return sum_ def assign_parent_child(stack): while len(stack) > 0: v, p = stack.pop() v.parent = p if p != None: v.child.remove(p) for c in v.child: stack.append((c, v)) def bfs(stack, pointer): while len(stack) > pointer: for c in stack[pointer].child: stack.append(c) pointer = pointer + 1 t = int(input()) while t > 0: t = t - 1 n = int(input()) arr = list(map(int, input().split())) nodes = [] for i in range(n): node = Node(arr[i], i) nodes.append(node) for q in range(n - 1): i, j = list(map(int, input().split())) i = i - 1 j = j - 1 nodes[i].child.append(nodes[j]) nodes[j].child.append(nodes[i]) stack = [] stack.append((nodes[0], None)) assign_parent_child(stack) stack = [nodes[0]] bfs(stack, 0) for i in range(len(stack) - 1, -1, -1): stack[i].gcd_of_tree() max_ = 1 stack = [] stack.append(nodes[0]) while len(stack) > 0: node = stack.pop() if len(node.child) != 0: curr = node.beauty() if max_ < curr: max_ = curr stack.extend(node.child) print(max_)
FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR VAR CLASS_DEF ASSIGN VAR NONE FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF IF VAR NONE RETURN VAR ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN VAR IF VAR NONE ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR IF VAR NONE EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF WHILE FUNC_CALL VAR VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
There is a rooted tree of N vertices rooted at vertex 1. Each vertex v has a value A_{v} associated with it. You choose a vertex v (possibly the root) from the tree and remove all vertices on the path from the root to the vertex v, also including v. This will result in a forest of zero or more connected components. The beauty of a connected component is the \mathrm{GCD} of the values of all vertices in the component. Find the maximum value of the sum of beauties of the obtained connected components for any choice of v. Here, \mathrm{GCD} stands for [Greatest Common Divisor]. ------ Input Format ------ - The first line contains a single integer T — the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N — the size of the tree. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the values associated with each vertex. - The next N-1 lines contain two space-separated integers u and v — denoting an undirected edge between nodes u and v. It is guaranteed that the edges given in the input form a tree. ------ Output Format ------ For each test case output the maximum value of the sum of beauties of the obtained connected components for any choice of v. ------ Constraints ------ $1 ≤ T ≤ 2\cdot10^{4}$ $1 ≤ N ≤ 3\cdot10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ $1 ≤ u,v ≤ N$ and $u \neq v$ - It is guaranteed that the edges given in the input form a tree. - The sum of $N$ over all test cases does not exceed $3\cdot10^{5}$ ----- Sample Input 1 ------ 1 10 15 30 15 5 3 15 3 3 5 5 1 2 1 5 2 3 2 4 5 6 5 7 5 8 7 9 7 10 ----- Sample Output 1 ------ 33 ----- explanation 1 ------ The tree from the sample is as follows. ![tree_{basic}] If vertex $v = 7$ is chosen, vertices $1$, $5$ and $7$ are removed. ![tree_{remove}] The resulting forest contains five connected components $\{8\}, \{6\}, \{10\}, \{9\}$ and $\{2, 3, 4\}$. ![tree_{value}] The beauties of the connected components are $3$, $15$, $5$, $5$ and $5$ respectively. Thus the answer is $3 + 15 + 5 + 5 + 5 = 33$. It can be shown that this is the maximum value possible for any choice of $v$.
import sys sys.setrecursionlimit(350000) def gcd(a, b): if a % b == 0: return b return gcd(b, a % b) def dps(node, parent): visited[node] = True dp[node] = dp[parent] - g_st[node] + g_sum[node] for child in adj[node]: if not visited[child]: if child != parent: dps(child, node) def dfs_2(node, parent): visited[node] = False for child in adj[node]: if visited[child]: if child != parent: g_sum[node] += g_st[child] dfs_2(child, node) def dfs(node, parent): visited[node] = True g_st[node] = A[node - 1] for child in adj[node]: if child != parent: if not visited[child]: if g_st[child] != 0: g_st[node] = gcd(g_st[node], g_st[child]) else: g_st[node] = gcd(g_st[node], dfs(child, node)) return g_st[node] for _ in range(int(input())): N = int(input()) A = [int(i) for i in input().split()] if N == 1: print(0) else: adj = {} for i in range(N): adj[i + 1] = [] for i in range(N - 1): u, v = [int(i) for i in input().split()] adj[u].append(v) adj[v].append(u) visited = [False] * (N + 1) g_st = [0] * (N + 1) g_sum = [0] * (N + 1) dp = [0] * (N + 1) dfs(1, 0) dfs_2(1, 0) dp[0] = g_st[1] dps(1, 0) print(max(dp))
IMPORT EXPR FUNC_CALL VAR NUMBER FUNC_DEF IF BIN_OP VAR VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR VAR VAR IF VAR VAR IF VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
There is a rooted tree of N vertices rooted at vertex 1. Each vertex v has a value A_{v} associated with it. You choose a vertex v (possibly the root) from the tree and remove all vertices on the path from the root to the vertex v, also including v. This will result in a forest of zero or more connected components. The beauty of a connected component is the \mathrm{GCD} of the values of all vertices in the component. Find the maximum value of the sum of beauties of the obtained connected components for any choice of v. Here, \mathrm{GCD} stands for [Greatest Common Divisor]. ------ Input Format ------ - The first line contains a single integer T — the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N — the size of the tree. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the values associated with each vertex. - The next N-1 lines contain two space-separated integers u and v — denoting an undirected edge between nodes u and v. It is guaranteed that the edges given in the input form a tree. ------ Output Format ------ For each test case output the maximum value of the sum of beauties of the obtained connected components for any choice of v. ------ Constraints ------ $1 ≤ T ≤ 2\cdot10^{4}$ $1 ≤ N ≤ 3\cdot10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ $1 ≤ u,v ≤ N$ and $u \neq v$ - It is guaranteed that the edges given in the input form a tree. - The sum of $N$ over all test cases does not exceed $3\cdot10^{5}$ ----- Sample Input 1 ------ 1 10 15 30 15 5 3 15 3 3 5 5 1 2 1 5 2 3 2 4 5 6 5 7 5 8 7 9 7 10 ----- Sample Output 1 ------ 33 ----- explanation 1 ------ The tree from the sample is as follows. ![tree_{basic}] If vertex $v = 7$ is chosen, vertices $1$, $5$ and $7$ are removed. ![tree_{remove}] The resulting forest contains five connected components $\{8\}, \{6\}, \{10\}, \{9\}$ and $\{2, 3, 4\}$. ![tree_{value}] The beauties of the connected components are $3$, $15$, $5$, $5$ and $5$ respectively. Thus the answer is $3 + 15 + 5 + 5 + 5 = 33$. It can be shown that this is the maximum value possible for any choice of $v$.
def gcd(a, b): while b: a, b = b, a % b return a T = int(input()) for _ in range(T): N = int(input()) vertices = list(map(int, input().split())) neighbours = [[] for _ in range(N)] children = [[] for _ in range(N)] for _ in range(N - 1): u, v = list(map(int, input().split())) neighbours[u - 1].append(v - 1) neighbours[v - 1].append(u - 1) q = [0] l = [] visited = [False] * N while q: u = q.pop() visited[u] = True children[u] = [v for v in neighbours[u] if not visited[v]] q.extend(children[u]) l.append(u) gcds = [None] * N for u in reversed(l): g = vertices[u] for v in children[u]: g = gcd(g, gcds[v]) gcds[u] = g above = [0] * N below = [0] * N for u in l: below[u] = sum(gcds[v] for v in children[u]) for v in children[u]: above[v] = above[u] + below[u] - gcds[v] m = max(above[u] + below[u] for u in l) print(m)
FUNC_DEF WHILE VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL 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(i: int, f: int) -> int: ans = 1 if i == finish else 0 for j in range(len(locations)): diff = abs(locations[j] - locations[i]) if j == i: continue elif diff <= f: ans += dfs(j, f - diff) return ans return dfs(start, fuel) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR FUNC_DEF 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 VAR IF VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_CALL VAR NONE 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: locs = locations dp = [([0] * (fuel + 1)) for _ in range(len(locs))] dp[start] = [1] * (fuel + 1) res = 0 for f in range(1, fuel + 1): for i in range(len(locs)): for j in range(len(locs)): if i != j: d = abs(locs[i] - locs[j]) if f - d >= 0: dp[i][f] = (dp[i][f] + dp[j][f - d]) % 1000000007 res = dp[finish][fuel] % 1000000007 return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN 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 BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP 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: mod = 10**9 + 7 @functools.lru_cache(None) def drive(i, fuel): res = 0 if i == finish: res += 1 for j in range(len(locations)): if i == j: continue dist = abs(locations[i] - locations[j]) if dist <= fuel: res += drive(j, fuel - dist) % mod return res % mod return drive(start, fuel)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF 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 BIN_OP FUNC_CALL VAR VAR BIN_OP 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) mem = {} mod = 10**9 + 7 def helper(ind, f): cnt = 0 if (ind, f) in mem: return mem[ind, f] if ind < 0 or ind > n - 1: return 0 if ind == finish and f >= 0: cnt += 1 for i in range(n): if i != ind and f - abs(locations[ind] - locations[i]) >= 0: cnt += helper(i, f - abs(locations[ind] - locations[i])) % mod mem[ind, f] = cnt return cnt % mod return helper(start, fuel) % mod
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR VAR RETURN VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN BIN_OP 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: mod = 10**9 + 7 def dp(cur, fuel): if fuel < 0: return 0 if (cur, fuel) in memo: return memo[cur, fuel] ans = 0 if cur == locations[finish]: ans = 1 for loc in locations: if loc != cur: ans += dp(loc, fuel - abs(cur - loc)) memo[cur, fuel] = ans return ans memo = {} return dp(locations[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 IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR DICT 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: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: n = len(locations) dp = [([0] * (fuel + 1)) for _ in range(n)] for i in range(fuel + 1): dp[finish][i] = 1 for i in range(1, fuel + 1): for j in range(n): for k in range(n): if j == k: continue dist = abs(locations[j] - locations[k]) if dist <= i: dp[j][i] += dp[k][i - dist] % (1000000000.0 + 7) return int(dp[start][fuel] % (1000000000.0 + 7))
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 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 ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP NUMBER NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP 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: MODULO = 10**9 + 7 def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: my_dp = dict() return Solution.get_dp(my_dp, locations, start, finish, fuel) @staticmethod def get_dp(dp, locations, start, finish, fuel): if (start, finish, fuel) not in dp: total_ways = 0 if start == finish: total_ways = 1 for idx, pos in enumerate(locations): if idx != start and abs(locations[start] - locations[idx]) <= fuel: total_ways = ( total_ways + Solution.get_dp( dp, locations, idx, finish, fuel - abs(locations[start] - locations[idx]), ) ) % Solution.MODULO dp[start, finish, fuel] = total_ways return dp[start, finish, fuel]
CLASS_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_DEF IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR 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 VAR ASSIGN VAR VAR VAR VAR VAR RETURN 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: @lru_cache(None) def dp(i, left): if left < 0: return 0 ret = i == finish for ni in range(len(locations)): if i == ni: continue ret += dp(ni, left - abs(locations[i] - locations[ni])) return ret return dp(start, fuel) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR 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 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 = {} def recursive(curr, finish, fuel): key = curr, fuel count = 0 if fuel < 0: return 0 elif curr == finish: count += 1 elif key in dp: return dp[key] count += sum( [ recursive(c, finish, fuel - abs(locations[c] - locations[curr])) for c in range(len(locations)) if c != curr ] ) dp[key] = count return dp[key] return recursive(start, finish, fuel) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR DICT FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER IF VAR VAR RETURN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR RETURN BIN_OP FUNC_CALL 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: def dp(curr_loc, fuel): if (curr_loc, fuel) in memo: return memo[curr_loc, fuel] if fuel < 0: return 0 ans = 0 if curr_loc == finish: ans = 1 for i, loc in enumerate(locations): if i != curr_loc: ans += dp(i, fuel - abs(locations[curr_loc] - loc)) memo[curr_loc, fuel] = ans return ans memo = {} return dp(start, fuel) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR 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 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 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: n = len(locations) dp = [([0] * (fuel + 1)) for _ in range(n)] dp[start][fuel] = 1 for f in range(fuel, 0, -1): for i in range(0, n): if dp[i][f] == 0 or abs(locations[i] - locations[finish]) > f: continue for j in range(0, n): d = abs(locations[i] - locations[j]) if i == j or d == 0 or d > f: continue dp[j][f - d] = (dp[j][f - d] + dp[i][f]) % 1000000007 return sum(dp[finish]) % 1000000007
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 ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER 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 cost(i, j): return abs(locations[i] - locations[j]) n, MOD = len(locations), 10**9 + 7 @functools.lru_cache(None) def solve(s, fuel): if fuel < 0: return 0 ans = 1 if s == finish else 0 for t in range(n): if s == t: continue ans += solve(t, fuel - cost(s, t)) return ans % MOD return solve(start, fuel)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL 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: MOD = 10**9 + 7 start, finish = locations[start], locations[finish] locations.sort() start, finish = locations.index(start), locations.index(finish) n = len(locations) @lru_cache(None) def dp(i, f): res = 0 if i == finish: res = 1 if f == 0: return res for j in range(1, i + 1): ff = locations[i] - locations[i - j] if ff <= f: res = (res + dp(i - j, f - ff)) % MOD else: break for j in range(1, n - i): ff = locations[i + j] - locations[i] if ff <= f: res = (res + dp(i + j, f - ff)) % MOD else: break return res return dp(start, fuel)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP 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, l: List[int], start: int, fin: int, fuel: int) -> int: a, b, i = l[start], l[fin], 0 while i < len(l): if abs(l[i] - a) + abs(l[i] - b) > fuel: del l[i] else: i += 1 if not l: return 0 n = len(l) start, fin = l.index(a), l.index(b) @lru_cache(None) def dfs(i: int, f: int) -> int: return ( 0 if f < 0 else (1 if i == fin else 0) + 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 VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF VAR VAR RETURN VAR NUMBER NUMBER 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 @lru_cache(None) def count_paths(index, fuel): total = 0 if fuel < 0: return 0 if index == finish: total += 1 current = locations[index] for next_index, x in enumerate(locations): if next_index != index: total += count_paths(next_index, max(-1, fuel - abs(current - x))) return total answer = count_paths(start, fuel) return answer % mod
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR FUNC_CALL VAR NONE ASSIGN 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: L = len(locations) @lru_cache(None) def move(loc, remaining): if remaining < 0: return 0 ans = 0 if loc == finish: ans += 1 for i in range(L): if i == loc: continue ans += move(i, remaining - abs(locations[i] - locations[loc])) return ans return move(start, fuel) % 1000000007 def countRoutes2( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: dp = defaultdict(dict) for j in range(0, fuel + 1): for i in range(len(locations)): if i == finish: ans = 1 else: ans = 0 for k in range(len(locations)): if k != i: f = j - abs(locations[i] - locations[k]) if f >= 0: ans += dp[k][f] dp[i][j] = ans % 1000000007 return dp[start][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 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 RETURN VAR FUNC_CALL VAR NONE RETURN BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL 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 VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER 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: start = locations[start] finish = locations[finish] locations.sort() res = 0 dict = {} def dfs(start, finish, fuel): if (start, fuel) in dict: return dict[start, fuel] route = 0 for val in locations: if val != start and abs(val - start) <= fuel: if val == finish: route += 1 dict[val, fuel - abs(val - start)] = dfs( val, finish, fuel - abs(val - start) ) route += dict[val, fuel - abs(val - start)] return route res = dfs(start, finish, fuel) if start == finish: res += 1 return res % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR 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: n = len(locations) sloc = sorted([(x, i) for i, x in enumerate(locations)]) froutes = [([0] * n) for _ in range(fuel + 1)] st, fn = -1, -1 for i in range(n): if sloc[i][1] == start: st = i if sloc[i][1] == finish: fn = i froutes[fuel][st] = 1 f0 = fuel while fuel > 0: for i, cnt in enumerate(froutes[fuel]): if cnt > 0: for j in range(i - 1, -1, -1): dist = sloc[i][0] - sloc[j][0] if dist <= fuel: froutes[fuel - dist][j] += cnt else: break for j in range(i + 1, n): dist = sloc[j][0] - sloc[i][0] if dist <= fuel: froutes[fuel - dist][j] += cnt else: break fuel -= 1 res = 0 for i in range(f0 + 1): res += froutes[i][fn] return res % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER 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
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, start, finish, fuel): if (start, fuel) in self.memo: return self.memo[start, fuel] count = 0 if start == finish: count += 1 for i, loc in enumerate(locations): dist = abs(locations[start] - loc) if i == start or dist > fuel: continue count += self.search(locations, i, finish, fuel - dist) self.memo[start, fuel] = count % (10**9 + 7) return self.memo[start, fuel] def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: self.memo = {} return self.search(locations, start, finish, fuel)
CLASS_DEF 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 ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR VAR VAR FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR DICT 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: cache = dict() def helper(i, f): if f < 0: return 0 if (i, f) not in cache: count = 1 if i == finish else 0 for j in range(len(locations)): if i != j: count += helper(j, f - abs(locations[i] - locations[j])) cache[i, f] = count return cache[i, f] return helper(start, fuel) % 1000000007
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF 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 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 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: q = [(start, fuel)] res = 0 if start == finish: res += 1 next_q = [] @lru_cache(None) def dp(cur_start, fuel): res = 0 for l in range(len(locations)): left_fuel = fuel - abs(locations[l] - locations[cur_start]) if l != cur_start and left_fuel >= 0: if l == finish: res += 1 if left_fuel > 0: res += dp(l, left_fuel) return res res += dp(start, fuel) return res % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR LIST FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_CALL VAR NONE 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: n = len(locations) dist = [[abs(locations[i] - locations[j]) for j in range(n)] for i in range(n)] min_d = [min(dist[i][j] for j in range(n) if j != i) for i in range(n)] memo = {} def dp(begin, left): if left < min_d[begin]: return 1 if begin == finish else 0 if (begin, left) in memo: return memo[begin, left] memo[begin, left] = sum( dp(i, left - dist[begin][i]) for i in range(n) if i != begin and left >= dist[begin][i] ) if begin == finish: memo[begin, left] += 1 return memo[begin, left] return dp(start, fuel) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR RETURN VAR VAR NUMBER NUMBER IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER 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_pos = locations[start] finish_pos = locations[finish] locations.sort() for i, location in enumerate(locations): if location == start_pos: start = i if location == finish_pos: finish = i print(locations) print((start, finish, fuel)) @lru_cache(None) def dfs(i, j, f): if f < 0: return 0 if abs(locations[i] - locations[j]) > f: return 0 l = locations[i] - f r = locations[i] + f li = bisect.bisect_left(locations, l) ri = bisect.bisect_right(locations, r) ans = 1 if i == j else 0 for k in range(li, ri): if k != i: ans += dfs(k, j, f - abs(locations[i] - locations[k])) return ans return dfs(start, finish, fuel) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR FUNC_CALL 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 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 dfs(start, fuel): if (start, fuel) not in cache: res = 0 for c in range(len(locations)): if c == start: continue dist = abs(locations[start] - locations[c]) if c == finish and dist <= fuel: res += 1 if dist <= fuel: res += dfs(c, fuel - dist) cache[start, fuel] = res % (10**9 + 7) return cache[start, fuel] return dfs(start, fuel) + int(start == finish)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR 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 IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR 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: shortest = {finish: 0} ans = 0 heap = sorted( [[abs(locations[i] - locations[finish]), i] for i in range(len(locations))] ) while len(shortest) < len(locations): d, i = heapq.heappop(heap) if i in shortest: continue shortest[i] = d for j in range(len(locations)): if j not in shortest and j != finish: heapq.heappush(heap, [abs(locations[j] - locations[i]) + d, j]) @lru_cache(None) def dfs(cur, fuel): if fuel < shortest[cur]: return 0 if fuel == 0: if cur != finish: return 0 else: return 1 ans = 0 if cur == finish: ans += 1 for i in range(len(locations)): cost = abs(locations[i] - locations[cur]) if i != cur and cost <= fuel: ans += dfs(i, fuel - cost) % 1000000007 return ans % 1000000007 return dfs(start, fuel)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR DICT VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR LIST BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR FUNC_DEF IF VAR VAR VAR RETURN NUMBER IF VAR NUMBER IF VAR VAR RETURN 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 VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER RETURN BIN_OP 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, A: List[int], start: int, end: int, fuel: int) -> int: mod = 10**9 + 7 cost = lambda a, b: abs(A[a] - A[b]) @functools.lru_cache(None) def route(start, fuel): nonlocal end if fuel < 0: return 0 r = 1 if start == end else 0 for i in range(len(A)): if i == start: continue r += route(i, fuel - cost(start, i)) % mod return r % mod answer = route(start, fuel) return answer
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR RETURN BIN_OP VAR VAR 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: array = [[(0) for i in range(len(locations))] for j in range(fuel + 1)] for fuel_idx in range(len(array)): array[fuel_idx][finish] = 1 for fuel_max in range(1, len(array)): for loc in range(len(array[0])): route_count = array[fuel_max][loc] for loc_idx in range(len(array[0])): if loc_idx == loc: continue fuel_res = fuel_max - abs(locations[loc] - locations[loc_idx]) if fuel_res < 0: continue route_count += array[fuel_res][loc_idx] array[fuel_max][loc] = route_count return array[fuel][start] % (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 FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER 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: a = locations n = len(a) MOD = 10**9 + 7 @lru_cache(maxsize=None) def dp(pos, fuel): res = 0 if pos == finish: res = 1 for k in range(n): dist = abs(a[pos] - a[k]) if dist > 0 and dist <= fuel: res = (res + dp(k, fuel - dist)) % MOD return res return dp(start, fuel)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER 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: self.locations = locations self.cache = [{} for i in range(len(locations))] re = self.dfs(start, finish, fuel) return re % (pow(10, 9) + 7) def dfs(self, start, tmpend, fu): if fu in list(self.cache[tmpend].keys()): return self.cache[tmpend][fu] if fu < 0: return 0 if fu == 0: self.cache[tmpend][0] = 0 if start != tmpend else 1 return self.cache[tmpend][0] self.cache[tmpend][fu] = 0 for i in range(len(self.locations)): if i == tmpend: continue self.cache[tmpend][fu] += self.dfs( start, i, fu - abs(self.locations[i] - self.locations[tmpend]) ) if start == tmpend: self.cache[tmpend][fu] += 1 return self.cache[tmpend][fu]
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR DICT VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN BIN_OP VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER VAR FUNC_DEF IF VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER NUMBER RETURN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL 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 VAR VAR 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, m = len(locations), 1000000007 s = [([0] * n) for _ in range(fuel + 1)] s[fuel][start] = 1 for i in range(fuel, 0, -1): for j in range(n): if abs(locations[j] - locations[finish]) <= i: for k in range(j): t = abs(locations[j] - locations[k]) if t <= i: s[i - t][k] = (s[i - t][k] + s[i][j]) % m for k in range(j + 1, n): t = abs(locations[j] - locations[k]) if t <= i: s[i - t][k] = (s[i - t][k] + s[i][j]) % m return sum([s[i][finish] for i in range(fuel + 1)]) % m
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER 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 IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FOR VAR FUNC_CALL 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 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 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: modulo = 10**9 + 7 n = len(locations) start_pos, finish_pos = locations[start], locations[finish] locations.sort() start = locations.index(start_pos) finish = locations.index(finish_pos) @lru_cache(None) def dp(cur, fuel): res = 0 if cur == finish: res += 1 for nxt in range(cur + 1, n): if nxt < 0 or nxt >= n: continue if abs(locations[nxt] - locations[cur]) > fuel: break res += dp(nxt, fuel - abs(locations[nxt] - locations[cur])) for nxt in range(cur - 1, -1, -1): if nxt < 0 or nxt >= n: continue if abs(locations[nxt] - locations[cur]) > fuel: break res += dp(nxt, fuel - abs(locations[nxt] - locations[cur])) return res % modulo return dp(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 VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER 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 FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER 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 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: @functools.lru_cache(None) def dp(pos, fuel): res = 1 if pos == locations[finish] else 0 for nxt in locations: if nxt == pos or fuel < abs(nxt - pos): continue res += dp(nxt, fuel - abs(nxt - pos)) return res % (10**9 + 7) return dp(locations[start], fuel)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP 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 countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: numCity = len(locations) dp = [([None] * numCity) for _ in range(fuel + 1)] def helper(city, fuel): if fuel < 0: return 0 res = dp[fuel][city] if res is not None: return res res = ( int(city == finish) + sum( helper(city2, fuel - abs(locations[city] - locations[city2])) for city2 in range(numCity) if city != city2 ) ) % (10**9 + 7) dp[fuel][city] = res return res return helper(start, fuel)
CLASS_DEF 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 FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR IF VAR NONE RETURN VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL 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
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, f: int) -> int: ans, n = 0, len(a) dp = [([0] * (f + 1)) for _ in range(n)] dp[s][0] = 1 for ff in range(f + 1): for i in range(n): for k in range(n): if i != k: dist = abs(a[i] - a[k]) if dist <= ff: dp[i][ff] += dp[k][ff - dist] ans += dp[e][ff] return ans % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP 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 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 countRoutesUtil(self, locations, currentLocation, target, fuelLeft, dp): if fuelLeft < 0: return 0 key = currentLocation, fuelLeft if key in dp: return dp[key] result = 0 if currentLocation == target: result += 1 for i in range(len(locations)): if i != currentLocation: result += self.countRoutesUtil( locations, i, target, fuelLeft - abs(locations[i] - locations[currentLocation]), dp, ) result %= 10**9 + 7 dp[key] = result % (10**9 + 7) return dp[key] def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: return self.countRoutesUtil(locations, start, finish, fuel, {})
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR IF VAR VAR RETURN 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 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 BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR VAR FUNC_DEF VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR 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: dp = [([-1] * (fuel + 1)) for i in range(len(locations))] def dfs(start, fuel): if fuel < 0: return 0 if dp[start][fuel] > -1: return dp[start][fuel] if start == finish: res = 1 else: res = 0 for i in range(len(locations)): if i != start: res += dfs(i, fuel - abs(locations[i] - locations[start])) dp[start][fuel] = res % (10**9 + 7) return dp[start][fuel] return dfs(start, fuel)
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 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 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 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) dp = [([0] * n) for _ in range(fuel + 1)] dp[fuel][start] = 1 M = int(1000000000.0 + 7) for f in range(fuel, 0, -1): for i in range(n): for j in range(i + 1, n): dis = abs(locations[i] - locations[j]) if dis <= f: dp[f - dis][j] = (dp[f - dis][j] + dp[f][i]) % M dp[f - dis][i] = (dp[f - dis][i] + dp[f][j]) % M ans = 0 for f in range(fuel + 1): ans += dp[f][finish] return ans % M
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR 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 ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER 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 IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER 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, L: List[int], st: int, fn: int, fuel: int) -> int: n = len(L) dp = [[(0) for i in range(fuel + 1)] for j in range(n)] dp[st][fuel] = 1 for f in range(fuel, -1, -1): for u in range(n): for v in range(n): if u != v: cost = abs(L[u] - L[v]) if f + cost <= fuel: dp[u][f] = dp[u][f] + dp[v][f + cost] return sum(dp[fn]) % 1000000007
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 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 BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR 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 memo = {} def dp(pos, leftFuel): if memo.get((pos, leftFuel), None) is not None: return memo[pos, leftFuel] ans = 0 if pos == finish: ans = 1 for otherPos in range(len(locations)): if ( pos != otherPos and abs(locations[pos] - locations[otherPos]) <= leftFuel ): ans += dp( otherPos, leftFuel - abs(locations[pos] - locations[otherPos]) ) memo[pos, leftFuel] = ans return ans ans = dp(start, fuel) % mod return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR DICT FUNC_DEF IF FUNC_CALL VAR VAR VAR NONE NONE 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 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 ASSIGN VAR VAR VAR VAR RETURN VAR 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: found = {} def count(cstart, cfuel): if (cstart, cfuel) in found: return found[cstart, cfuel] if cfuel < 0: return 0 if cfuel == 0: return cstart == finish r = int(cstart == finish) for i, l in enumerate(locations): if i != cstart: r += count(i, cfuel - abs(locations[i] - locations[cstart])) found[cstart, cfuel] = r % (10**9 + 7) return found[cstart, cfuel] return count(start, fuel)
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 IF VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR 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 VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP 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, start: int, finish: int, fuel: int) -> int: MOD = 10**9 + 7 dp = [([-1] * (len(locations) + 1)) for _ in range(fuel + 1)] def recur(st, ed, f): if f < 0: return 0 if dp[f][st] != -1: return dp[f][st] total = 0 for i in range(len(locations)): if i != st: total += recur(i, ed, f - abs(locations[st] - locations[i])) if st == ed: answer = (total + 1) % MOD else: answer = total % MOD dp[f][st] = answer return answer recur(start, finish, fuel) return dp[fuel][start]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN 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 IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR EXPR FUNC_CALL 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, start, finish, fuel): if abs(locations[start] - locations[finish]) > fuel: return 0 l = len(locations) dp = [[(0) for k in range(fuel + 1)] for j in range(l)] dp[start][0] = 1 for k in range(1, fuel + 1): for i in range(l): for j in range(l): if i != j: cost = abs(locations[i] - locations[j]) if k - cost >= 0: dp[i][k] += dp[j][k - cost] ans = 0 for i in range(fuel + 1): ans += dp[finish][i] return ans % (10**9 + 7)
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR RETURN 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 NUMBER 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 FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR BIN_OP 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
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 = len(locations) dp = [(M * [0]) for _ in range(fuel + 1)] dp[0][start] = 1 Z = 10**9 + 7 for i in range(1, fuel + 1): for j in range(M): for k in range(M): dis = abs(locations[k] - locations[j]) if 0 < dis <= i: dp[i][j] += dp[i - dis][k] dp[i][j] %= Z total = sum(dp[i][finish] for i in range(1, fuel + 1)) % Z if finish == start: return total + 1 else: return total
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR LIST NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR IF VAR VAR RETURN BIN_OP 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: @lru_cache(None) def helper(startInd, remainFuel): if remainFuel < 0: return 0 if abs(locations[finish] - locations[startInd]) > remainFuel: return 0 if remainFuel == 0: return 1 res = 1 if startInd == finish else 0 for i in range(0, len(locations)): if ( i == startInd or abs(locations[i] - locations[startInd]) > remainFuel ): continue res += helper(i, remainFuel - abs(locations[i] - locations[startInd])) return res % (10**9 + 7) return helper(start, fuel)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER 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 BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER 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
def neigh(start, locations, fuel): res = [] n = len(locations) for j in range(n): i = locations[j] if j == start: continue if abs(i - locations[start]) <= fuel: res.append(j) return res class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: if abs(locations[finish] - locations[start]) > fuel: return 0 n = len(locations) dp = [[(-1) for i in range(fuel + 1)] for j in range(n)] def dfs(start, fuel): if fuel < 0: return 0 if dp[start][fuel] != -1: return dp[start][fuel] neighs = neigh(start, locations, fuel) count = 0 if start == finish: count += 1 for i in neighs: count += dfs(i, fuel - abs(locations[start] - locations[i])) dp[start][fuel] = count return count return dfs(start, fuel) % (pow(10, 9) + 7)
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR 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 NUMBER VAR FUNC_CALL VAR 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 FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER FOR 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 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
sys.setrecursionlimit(1000000) class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: MOD = 10**9 + 7 start, finish = locations[start], locations[finish] if abs(finish - start) > fuel: return 0 locations = [x for x in locations if abs(x - start) <= fuel] @lru_cache(None) def dfs(u, fuel): res = 0 if u == finish: res += 1 for v in locations: if v != u: cost = abs(v - u) if cost <= fuel: res += dfs(v, fuel - cost) return res % MOD return dfs(start, fuel)
EXPR FUNC_CALL VAR NUMBER CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR BIN_OP 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: mod = 10**9 + 7 n = len(locations) @lru_cache(None) def dfs(cur, remain): if abs(locations[finish] - locations[cur]) > remain: return 0 res = 0 if cur == finish: res = 1 for i in range(n): if i == cur: continue temp = abs(locations[i] - locations[cur]) if temp <= remain: res = (res + dfs(i, remain - temp)) % mod return res return dfs(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 FUNC_DEF IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER 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 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: dp = [[(-1) for _ in range(fuel + 1)] for _ in locations] def search(start, fuel): if fuel == 0: if start == finish: return 1 return 0 if dp[start][fuel] != -1: return dp[start][fuel] dp[start][fuel] = 1 if start == finish else 0 for i in range(len(locations)): left = fuel - abs(locations[i] - locations[start]) if i != start and left >= 0: dp[start][fuel] += search(i, left) return dp[start][fuel] % 1000000007 search(start, fuel) return dp[start][fuel] % 1000000007
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF IF VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN BIN_OP 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 pos_start = locations[start] pos_finish = locations[finish] locations.sort() start = locations.index(pos_start) finish = locations.index(pos_finish) @lru_cache(maxsize=None) def find(start, fuel): count = 0 if abs(start - finish) == 1 and fuel == abs( locations[start] - locations[finish] ): return 1 if locations[start] == locations[finish]: count += 1 if fuel >= abs(locations[start] - locations[finish]): for i in range(start - 1, -1, -1): if ( abs(locations[start] - locations[i]) + abs(locations[i] - locations[finish]) <= fuel ): count += find(i, fuel - abs(locations[start] - locations[i])) else: break for j in range(start + 1, len(locations)): if ( abs(locations[start] - locations[j]) + abs(locations[j] - locations[finish]) <= fuel ): count += find(j, fuel - abs(locations[start] - locations[j])) else: break return count return find(start, fuel) % MOD
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP FUNC_CALL VAR BIN_OP VAR VAR 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 FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR BIN_OP VAR VAR 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 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: num_paths = [[(0) for _ in locations] for _ in range(fuel + 1)] mod_constant = 10**9 + 7 num_paths[0][finish] = 1 for this_fuel in range(1, fuel + 1): for i, loci in enumerate(locations): this_num_paths = 0 for j, locj in enumerate(locations): if i == j: continue if abs(loci - locj) <= this_fuel: this_num_paths = ( this_num_paths + num_paths[this_fuel - abs(loci - locj)][j] ) % mod_constant num_paths[this_fuel][i] = this_num_paths total_num_paths = 0 for this_fuel in range(fuel + 1): total_num_paths = ( total_num_paths + num_paths[this_fuel][start] ) % mod_constant return total_num_paths
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER 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
sys.setrecursionlimit(1000000) class Solution: def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: MOD = 10**9 + 7 N = len(locations) dp = [([0] * (fuel + 1)) for _ in range(N)] dp[finish] = [1] * (fuel + 1) for f in range(1, fuel + 1): for u in range(N): for v in range(N): if u != v: delta = abs(locations[u] - locations[v]) if delta <= f: dp[u][f] += dp[v][f - delta] dp[u][f] %= MOD return dp[start][fuel]
EXPR FUNC_CALL VAR NUMBER 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 BIN_OP LIST NUMBER BIN_OP 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 FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR 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: DP = [([0] * len(locations)) for _ in range(fuel + 1)] DP[fuel][start] = 1 for i in range(fuel - 1, -1, -1): for j in range(len(locations)): for k in range(len(locations)): if j == k: continue past = i + abs(locations[k] - locations[j]) if past > fuel: DP[i][j] += 0 else: DP[i][j] += DP[past][k] return sum(row[finish] for row in DP) % (10**9 + 7)
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 VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR VAR VAR 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: MOD = 10**9 + 7 n = len(locations) graph = defaultdict(list) for i in range(n): for j in range(i + 1, n): cost = abs(locations[i] - locations[j]) graph[i].append((j, cost)) graph[j].append((i, cost)) @lru_cache(None) def dfs(i, budget): if budget < 0: return 0 res = 1 if i == finish else 0 if budget == 0: return res for nei, cost in graph[i]: res += dfs(nei, budget - cost) res %= MOD return res res = dfs(start, fuel) return res % 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 ASSIGN 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 EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER IF VAR NUMBER RETURN VAR FOR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR NONE ASSIGN 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: md = 10**9 + 7 n = len(locations) dp = [([0] * (fuel + 1)) for _ in range(n)] dp[start][0] = 1 for j in range(fuel): for i in range(n): pre = dp[i][j] % md if pre == 0: continue for ni in range(n): if i == ni: continue nj = j + abs(locations[i] - locations[ni]) if nj > fuel: continue dp[ni][nj] += pre ans = sum(dp[finish]) % md return ans
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 FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER 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 VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR