Datasets:

inputs
stringlengths
50
14k
targets
stringlengths
4
655k
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: N = len(arr) @lru_cache(None) def rec(idx): if not 0 <= idx < N: return -inf # print(idx) ans = 1 for i in range(idx-1, idx-d-1, -1): if i < 0 or arr[i] >= arr[idx]: break ans = max(ans, rec(i) + 1) for i in range(idx+1, idx+d+1): if i >= N or arr[i] >= arr[idx]: break ans = max(ans, rec(i) + 1) return ans ans = 0 for i in range(N): ans = max(ans, rec(i)) return ans
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: @lru_cache(None) def dfs(i): if i==0 and i+1<len(arr): if arr[i]<=arr[i+1]: return 1 elif i==len(arr)-1 and i-1>=0: if arr[i]<=arr[i-1]: return 1 elif i-1>=0 and i+1<len(arr) and arr[i]<=arr[i-1] and arr[i]<=arr[i+1]: return 1 left_step = 1 for j in range(i+1, i+d+1): if j>=len(arr) or arr[j]>=arr[i]: break left_step = max(left_step, 1+dfs(j)) right_step = 1 for j in range(i-1, i-d-1, -1): if j<0 or arr[j]>=arr[i]: break right_step = max(right_step, 1+dfs(j)) return max(left_step, right_step) max_step = 0 for i in range(len(arr)): max_step = max(max_step, dfs(i)) # print(dfs(i)) return max_step # d限定了可能跳的最大步长 # 在步长范围内,跳的距离必须满足中间不能有大于自己的值 # 返回可能到达的最多的下标 # 每个位置有自己可能调到的最多下标 # 比如6-->4 # 14-》8-》6 或者14-》6-》4 # 长度为1000 # 按照高度大小,求解初始跳的步长 # 最终一定是跳到一个山谷或者两端的位置 # dp[i]: 当位置到达下标i时,可能跳到的最多下标,对于山谷或者两端位置而言,初始值为1
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: from functools import lru_cache n = len(arr) @lru_cache(maxsize=None) def f(i): res = 1 for j in range(i + 1, min(i + d + 1, n)): if arr[j] < arr[i]: res = max(res, 1 + f(j)) else: break for j in range(i - 1, max(-1, i - d - 1), -1): if arr[j] < arr[i]: res = max(res, 1 + f(j)) else: break return res return max(f(_) for _ in range(n))
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, A: List[int], d: int) -> int: n = len(A) @lru_cache(None) def helper(i): ans = 1 l = max(0,i-d) r = min(n-1,i+d) #print(i,l,r) for j in reversed(range(l,i)): if(A[j]<A[i]): ans = max(ans,1 + helper(j)) else: break for j in range(i+1,r+1): if(A[j]<A[i]): ans = max(ans,1 + helper(j)) else: break return ans ans = max([helper(i) for i in range(n)]) return ans
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: sorted_indexes = sorted(range(len(arr)), key = lambda i:arr[i]) # print(sorted_indexes) n = len(arr) dp = [1] * n def get_neighs(cur): neighs = [] for i in range(cur + 1, min(cur + d + 1, n)): if arr[i] < arr[cur]: neighs.append(i) else: break for i in range(cur - 1, max(cur - d - 1, -1), -1): if arr[i] < arr[cur]: neighs.append(i) else: break return neighs for cur in sorted_indexes: neighs = get_neighs(cur) for neigh in neighs: dp[cur] = max(dp[cur], dp[neigh] + 1) return max(dp)
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps2(self, arr: List[int], d: int) -> int: n = len(arr) res = [0] * n def dp(i): if res[i] > 0: return res[i] res[i]=1 # at least itself for di in [-1, 1]: for j in range(i+di, i+d*di+di, di): # di=-1, j in range(i-1, i-d-1, -1) which is from i-1 to i-2 to ... i-d (d positions, all are reachable) # di=1, j in range(i+1, i+d+1, 1), which is from i+1 to i+2 to ... i+d ( d positions, all are reachable) if not (0<=j<n and arr[j] < arr[i]): # if j<0 or j>=n or arr[j] >= arr[i]: break break res[i] = max(res[i], dp(j) + 1) return res[i] return max(map(dp, range(n))) def maxJumps1(self, arr, d): # 最直观的dp方法!递归调用+记忆化保存 n = len(arr) res = [0] * n def dp(i): if res[i] > 0: return res[i] res[i] = 1 for j in range(i+1, i+d+1): if j>=n or arr[j] >= arr[i]: break res[i] = max(res[i], dp(j) + 1) for j in range(i-1, i-d-1, -1): if j<0 or arr[j] >= arr[i]: break res[i] = max(res[i], dp(j) + 1) return res[i] return max(dp(i) for i in range(n)) def maxJumps(self, arr, d): n = len(arr) dp = [1] * n for a, i in sorted([a, i] for i, a in enumerate(arr)): for di in [-1, 1]: for j in range(i + di, i + d*di+di, di): if not (0<=j<n and arr[j] < arr[i]): break dp[i] = max(dp[i], dp[j] + 1) return max(dp)
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: hp = [(v, i) for i, v in enumerate(arr)] heapq.heapify(hp) dp = [0] * len(arr) res = 0 while hp: h, i = heapq.heappop(hp) tmp = 1 for j in range(i - 1, i - 1 - d, -1): if j < 0 or arr[j] >= arr[i]: break tmp = max(tmp, dp[j] + 1) for j in range(i + 1, i + 1 + d): if j >= len(arr) or arr[j] >= arr[i]: break tmp = max(tmp, dp[j] + 1) dp[i] = tmp return max(dp)
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: dp = [1] * len(arr) a = [] for idx, val in enumerate(arr): a.append((val, idx)) a.sort() print(a) for _, idx in a: for i in range(1, d+1): if idx+i<len(arr) and arr[idx+i]<arr[idx]: dp[idx] = max(dp[idx], dp[idx+i]+1) else: break for i in range(1, d+1): if 0<=idx-i and arr[idx-i]<arr[idx]: dp[idx] = max(dp[idx], dp[idx-i]+1) else: break return max(dp)
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, A: List[int], d: int) -> int: n = len(A) dp = [1] * (n + 1) stack = [] for i, a in enumerate(A + [float('inf')]): while stack and A[stack[-1]] < a: L = [stack.pop()] while stack and A[stack[-1]] == A[L[0]]: L.append(stack.pop()) for j in L: if i - j <= d: dp[i] = max(dp[i], dp[j] + 1) if stack and j - stack[-1] <= d: dp[stack[-1]] = max(dp[stack[-1]], dp[j] + 1) stack.append(i) return max(dp[:-1])
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: #top-down dp res = [1] * len(arr) for a, i in sorted([a, i] for i, a in enumerate(arr)): for di in [-1, 1]: for j in range(i + di, i + d * di + di, di): if not (0 <= j < len(arr) and arr[j] < arr[i]): break res[i] = max(res[i], 1 + res[j]) return max(res)
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: dp = [0] * len(arr) path = {i: [(i, arr[i])] for i in range(len(arr))} def jump(i): nonlocal dp, path if dp[i] != 0: return dp[i] left, right = 0, 0 nl, nr = i, i for j in range(i+1, i+d+1): if j >= len(arr): break if arr[j] < arr[i]: t= jump(j) if right < t: right = t nr = j else: break for j in range(i-1, i-d-1, -1): if j < 0: break if arr[j] < arr[i]: t = jump(j) if left < t: left = t nl = j else: break if left < right and right > 0: path[i] += path[nr] elif left >= right and left > 0: path[i] += path[nl] dp[i] = max(left, right) + 1 return dp[i] ans = 0 for i in range(len(arr)): jump(i) # print(i, path[i]) ans = max(ans, dp[i]) return ans
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
import functools class Solution: def maxJumps(self, arr: List[int], d: int) -> int: @functools.lru_cache(None) def jump(index): res = 0 for direction in [-1, 1]: for x in range(1, d + 1): j = index + x * direction if 0 <= j < len(arr) and arr[j] < arr[index]: res = max(res, jump(j)) else: break return res + 1 return max(jump(index) for index in range(len(arr)))
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: N = len(arr) dp = [None]* N def cdp(i): if dp[i] != None: return dp[i] dp[i] = 1 for j in range(i-1,max(0, i-d) -1 , -1): if arr[j] >= arr[i]: break dp[i] = max(dp[i] , 1 + cdp(j)) for j in range(i+1,min(N-1, i+d) +1): if arr[j] >= arr[i]: break dp[i] = max(dp[i] , 1 + cdp(j)) return dp[i] for i in range(N): cdp(i) return max(dp)
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
from functools import lru_cache class Solution: def maxJumps(self, arr, d): n = len(arr) @lru_cache(maxsize = None) def findMaxReach(index): best = 1 for i in range(1, d + 1): curIndex = index - i if 0 <= curIndex < n and arr[curIndex] < arr[index]: best = max(findMaxReach(curIndex) + 1, best) else: break for i in range(1, d + 1): curIndex = index + i if 0 <= curIndex < n and arr[curIndex] < arr[index]: best = max(findMaxReach(curIndex) + 1, best) else: break return best ans = 0 for i in range(n): ans = max(findMaxReach(i), ans) return ans
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: N = len(arr) dp = [None]* N def cdp(i): if dp[i] !=None: return dp[i] dp[i] = 1 for j in range(i-1, max(0, i-d) -1 , -1): if arr[j] >= arr[i]: break dp[i] = max(dp[i] , 1 + cdp(j)) for j in range(i+1, min(N-1, i+d) +1 ): if arr[j] >= arr[i]: break dp[i] = max(dp[i] , 1 + cdp(j)) return dp[i] for i in range(N): cdp(i) return max(dp)
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
from functools import lru_cache class Solution: def maxJumps(self, arr: List[int], d: int) -> int: n = len(arr) @lru_cache(maxsize=None) def jump(i): res = 1 def visit(rng): nonlocal res for j in rng: if not 0 <= j < n or arr[j] >= arr[i]: break res = max(res, 1 + jump(j)) visit(range(i - 1, i - d - 1, -1)) visit(range(i + 1, i + d + 1)) return res return max([jump(i) for i in range(n)])
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: dp = [0 for i in range(len(arr))] def findMaxJumps(i): if dp[i]!=0: return dp[i] else: ans = 1 for j in range(i+1, min(i+d+1, len(arr))): if (arr[j]>=arr[i]): break ans = max(ans, 1 + findMaxJumps(j)) for j in range(i-1, max(i-d,0)-1, -1): if (arr[j]>=arr[i]): break ans = max(ans, 1+ findMaxJumps(j)) dp[i] = ans return ans ans = 1 for i in range(len(arr)): ans = max(ans, findMaxJumps(i)) return ans
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
# 1340. Jump Game V class Solution2: # bottom-up dp # Time complexity O(NlogN + ND), where we are given D <= N # Space complexity O(N) for dp def maxJumps(self, arr, d): n = len(arr) dp = [1] * n for a, i in sorted([a, i] for i, a in enumerate(arr)): for di in [-1, 1]: for j in range(i+di, i+di+d*di, di): if not (0 <= j < n and arr[j] < arr[i]): break dp[i] = max(dp[i], dp[j] + 1) return max(dp) class Solution: # All element are pushed in and poped from stack1&2 only once, # strick O(N) time. 复杂度严格符合O(N)。 # Decreasing Stack + DP # Use a stack to keep the index of decreasing elements. # Iterate the array A. When we meet a bigger element a, # we pop out the all indices j from the top of stack, # where A[j] have the same value. # Then we update dp[i] and dp[stack[-1]]. # Since all indices will be pushed and popped in stack, # appended and iterated in L once, # the whole complexity is guaranteed to be O(N). # All element are pushed in and poped from stack1&2 only once, strick O(N) time. # Time complexity O(N); Space complexity O(N) # Python runtime 120ms, beats 100% def maxJumps(self, arr, d): n = len(arr) dp = [1] * (n + 1) stack = [] for i, a in enumerate(arr + [float('inf')]): while stack and arr[stack[-1]] < a: L = [stack.pop()] while stack and arr[stack[-1]] == arr[L[0]]: L.append(stack.pop()) for j in L: if i - j <= d: dp[i] = max(dp[i], dp[j] + 1) if stack and j - stack[-1] <= d: dp[stack[-1]] = max(dp[stack[-1]], dp[j] + 1) stack.append(i) return max(dp[:-1]) class Solution1: # top-down dp # Time complexity O(ND) # Space complexity O(N) for dp def maxJumps(self, arr: List[int], d: int) -> int: n = len(arr) res = [0] * n def dp(i): if res[i]: return res[i] res[i] = 1 for di in [-1, 1]: for j in range(i+di, i+di+d*di, di): if not (0 <= j < n and arr[j] < arr[i]): break res[i] = max(res[i], dp(j)+1) return res[i] return max(list(map(dp, list(range(n))))) # range(0, n) as input of dp func, then get the max of their results
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: sorted_indexes = sorted(range(len(arr)), key = lambda i:arr[i]) n = len(arr) dp = [1] * n def get_neighs(cur): neighs = [] directions = [1, -1] for dire in directions: for i in range(cur + dire, cur + (d + 1) * dire, dire): if i < 0 or i >= n: break if arr[i] >= arr[cur]: break neighs.append(i) return neighs for cur in sorted_indexes: for neigh in get_neighs(cur): dp[cur] = max(dp[cur], dp[neigh] + 1) return max(dp)
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: n=len(arr) if n==1 or d==0: return 1 e=[[]for i in range(n)] for i in range(0,n): for j in range(i-1,max(0,i-d)-1,-1): if arr[j]<arr[i]: e[i].append(j) else : break; for j in range(i+1,min(n-1,i+d)+1): if arr[j]<arr[i]: e[i].append(j) else : break; ans=1 queue=[] cnt=[1for i in range(n)] for i in range(0,n): queue.append([arr[i],i]) queue.sort() for i in range(0,n): u=queue[i][1] for v in e[u]: cnt[u]=max(cnt[u],cnt[v]+1) ans=max(ans,cnt[u]) return ans
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: sorted_indexes = sorted(range(len(arr)), key = lambda i:arr[i]) n = len(arr) dp = [1] * n def get_neighs(cur): neighs = [] directions = [1, -1] for dire in directions: for i in range(cur + dire, cur + (d + 1) * dire, dire): if i < 0 or i >= n: break if arr[i] >= arr[cur]: break neighs.append(i) return neighs for cur in sorted_indexes: neighs = get_neighs(cur) for neigh in neighs: dp[cur] = max(dp[cur], dp[neigh] + 1) return max(dp)
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: n = len(arr) @lru_cache(None) def dfs(i): left, right = i, i max_range = 1 while left - 1 >= 0 and arr[left - 1] < arr[i] and left - 1 >= i - d: left -= 1 while right + 1 < n and arr[right + 1] < arr[i] and right + 1 <= i + d: right += 1 for nxt in range(left, right + 1): if nxt != i: max_range = max(max_range, 1 + dfs(nxt)) return max_range return max(dfs(i) for i in range(n))
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: v = [] n = len(arr) for i in range(n): v.append((arr[i], i)) v = sorted(v) f = [-1] * n ans = -1 for i in range(n): idx = v[i][1] f[idx] = 1 for j in range(1, d+1): if idx - j >= 0 and arr[idx - j] < arr[idx]: f[idx] = max(f[idx], f[idx - j] + 1) else: break for j in range(1, d+1): if idx + j < n and arr[idx + j] < arr[idx]: f[idx] = max(f[idx], f[idx + j] + 1) else: break ans = max(ans, f[idx]) return ans
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
def bfs(arr,i,d,memo): if i in memo: return memo[i] res = 1 for j in range(max(i-1,0),max(i-d-1,-1),-1): if arr[j]>=arr[i]: break res = max(res,bfs(arr,j,d,memo)+1) for j in range(i+1,min(i+d+1,len(arr))): if arr[j]>=arr[i]: break res = max(res,bfs(arr,j,d,memo)+1) #print(i,res) memo[i] = res return res class Solution: def maxJumps(self, arr: List[int], d: int) -> int: memo = dict() return max( bfs(arr,i,d,memo) for i in range(len(arr)) )
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: N = len(arr) dp = [None] * N def cdp(i): if dp[i]: return dp[i] dp[i]=1 for j in range(i-1, max(0, i-d)-1, -1): if arr[j] >= arr[i]: break dp[i] = max(dp[i] , 1 + cdp(j)) for j in range(i+1, min(N-1, i+d)+1, +1): if arr[j] >= arr[i]: break dp[i] = max(dp[i] , 1 + cdp(j)) return dp[i] for i in range(N): cdp(i) return max(dp)
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: n = len(arr) dp = [1 for i in range(n)] @lru_cache(None) def rec(i): j=1 while(j<=d and i-j>=0 and arr[i-j]<arr[i]): dp[i] = max(dp[i], 1+rec(i-j)) j += 1 j=1 while(j<=d and i+j<n and arr[i+j]<arr[i]): dp[i] = max(dp[i], 1+rec(i+j)) j += 1 return dp[i] for k in range(n): rec(k) return max(dp)
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: n = len(arr) res = [1] * n for a, i in sorted([a, i] for i, a in enumerate(arr)): for di in [-1, 1]: for j in range(i + di, i + d * di + di, di): if not (0 <= j < n and arr[j] < arr[i]): break res[i] = max(res[j] + 1, res[i]) return max(res)
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: # stack, TC: O(N), SC: O(N). A, n = arr, len(arr) # stack is index with decreasing of values, pending jumpping from right, being popped when meet next wall on right. s, stack = [1] * (n + 1), [] for i, x in enumerate(A + [float('inf')]): while stack and A[stack[-1]] < x: queue = [stack.pop()] while stack and A[stack[-1]] == A[queue[-1]]: queue.append(stack.pop()) # j is min between stack[-1] and i, reverse jump up to stack[-1] and i for j in queue: if i - j <= d: s[i] = max(s[i], s[j] + 1) if stack and j - stack[-1] <= d: s[stack[-1]] = max(s[stack[-1]], s[j] + 1) stack.append(i) return max(s[:-1])
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: visited = {} arrLen = len(arr) def check(i0): if i0 in visited: return visited[i0] di = 1 preV = 0 subAns = 1 while di <= d and i0+di < arrLen and arr[i0+di] < arr[i0]: if arr[i0+di] >= preV: subAns = max(subAns, 1 + check(i0 + di)) preV = max(preV, arr[i0+di]) di += 1 di = 1 preV = 0 while di <= d and i0-di >= 0 and arr[i0-di] < arr[i0]: if arr[i0-di] >= preV: subAns = max(subAns, 1 + check(i0 - di)) preV = max(preV, arr[i0-di]) di += 1 visited[i0] = subAns return subAns ans = 1 for i in range(arrLen): ans = max(ans, check(i)) # print(visited) return ans
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: N = len(arr) dp = [None]* N def cdp(i): if dp[i] != None: return dp[i] dp[i] = 1 for j in range(i-1, max(0,i-d)-1,-1): if arr[j]>= arr[i]: break dp[i] = max(dp[i] , 1+cdp(j)) for j in range(i+1, min(N-1,i+d)+1): if arr[j]>= arr[i]: break dp[i] = max(dp[i] , 1+cdp(j)) return dp[i] for i in range(N): cdp(i) return max(dp)
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: res = 0 n = len(arr) self.m = [0] * n for i in range(n): ans = self.dfs(arr, d, i) res = max(ans, res) return res def dfs(self, arr, d, i): res = 1 if self.m[i] != 0: return self.m[i] for k in range(1, d+1): if i + k >= len(arr): break if arr[i+k] >= arr[i]: break ans = self.dfs(arr, d, i+k) + 1 if ans > res: res = ans for k in range(1, d+1): if i - k < 0: break if arr[i-k] >= arr[i]: break ans = self.dfs(arr, d, i-k) + 1 if ans > res: res = ans self.m[i] = res return res
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, A: List[int], d: int) -> int: n = len(A) dp = [1] * n for a, i in sorted([a, i] for i, a in enumerate(A)): j = i - 1 while j >= 0 and A[j] < A[i] and i - j <= d: dp[i] = max(dp[i], dp[j] + 1) j -= 1 j = i + 1 while j < n and A[j] < A[i] and j - i <= d: dp[i] = max(dp[i], dp[j] + 1) j += 1 return max(dp)
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: n = len(arr) if(n<=1): return n visited = [False for i in range(n)] L_neighbor = {} R_neighbor = {} #limit = {} for i in range(n): L_neighbor[i] = i R_neighbor[i] = i dec = deque() dec.append(0) for i in range(1, n): while(len(dec)>0): if(dec[0]<i-d): dec.popleft() else: break while(len(dec)>0): if(arr[dec[len(dec)-1]]<arr[i]): dec.pop() else: break if(len(dec)==0): L_neighbor[i] = max(i-d, 0) dec.append(i) else: L_neighbor[i] = max(dec[len(dec)-1]+1, i-d, 0) dec.append(i) dec = deque() dec.append(n-1) for i in range(n-2, -1, -1): while(len(dec)>0): if(dec[0]>i+d): dec.popleft() else: break while(len(dec)>0): if(arr[dec[len(dec)-1]]<arr[i]): dec.pop() else: break if(len(dec)==0): R_neighbor[i] = min(i+d, n-1) dec.append(i) else: R_neighbor[i] = min(dec[len(dec)-1]-1, i+d, n-1) dec.append(i) res = 0 limit = [0 for i in range(n)] for i in range(n): self.maxJumpDFS(i, arr, L_neighbor, R_neighbor, visited, limit) for i in range(n): if(res<limit[i]): res = limit[i] return res def maxJumpDFS(self, start, arr, L_neighbor, R_neighbor, visited, limit): if(visited[start]): return limit[start] else: currmax = 0 for i in range(L_neighbor[start], R_neighbor[start]+1): if(i==start): continue curr = self.maxJumpDFS(i, arr, L_neighbor, R_neighbor, visited, limit) if(currmax<curr): currmax = curr visited[start] = True limit[start] = currmax + 1 return limit[start]
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: maxPath = 0 memory = {i:0 for i in range(len(arr))} for i in range(len(arr)): maxPath = max(maxPath, self.dfs(i, d, arr, len(arr), memory)) return maxPath def dfs(self, index, d, arr, arrLen, memory): if memory[index] != 0: return memory[index] currentMax = 0 for i in range(index+1, index +d +1): if i >= arrLen or arr[index] <= arr[i]: break currentMax = max(currentMax, self.dfs(i, d, arr, arrLen, memory)) for i in range(index-1, index - d - 1, -1): if i < 0 or arr[index] <= arr[i]: break currentMax = max(currentMax, self.dfs(i, d, arr, arrLen, memory)) memory[index] = currentMax+1 return memory[index]
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: n = len(arr) i2h = {} for i, h in enumerate(arr): i2h[i] = (h, i) dp = [1] * n for h, idx in sorted(i2h.values(), key=lambda hi: hi[0]): j = idx + 1 while j <= min(n - 1, idx + d) and arr[idx] > arr[j]: dp[idx] = max(dp[idx], dp[j] + 1) j += 1 j = idx - 1 while j >= max(0, idx - d) and arr[idx] > arr[j]: dp[idx] = max(dp[idx], dp[j] + 1) j -= 1 return max(dp) def maxJumps__(self, arr: List[int], d: int) -> int: n = len(arr) memo = [0] * n def dfs(idx): if memo[idx]: return memo[idx] jump = 1 j = idx + 1 while j <= min(n - 1, idx + d) and arr[idx] > arr[j]: jump = max(jump, dfs(j) + 1) j += 1 j = idx - 1 while j >= max(0, idx - d) and arr[idx] > arr[j]: jump = max(jump, dfs(j) + 1) j -= 1 memo[idx] = jump return jump max_val = 0 for i in range(n): max_val = max(max_val, dfs(i)) return max_val def maxJumps_(self, arr: List[int], d: int) -> int: def backtracking(idx): nonlocal memo, max_val if idx in memo: return memo[idx] lstack = [] rstack = [] lbarrier = False rbarrier = False for i in range(1, d + 1): if not lbarrier and idx - i >= 0 and arr[idx - i] < arr[idx]: lstack.append(idx - i) else: lbarrier = True if not rbarrier and idx + i < len(arr) and arr[idx + i] < arr[idx]: rstack.append(idx + i) else: rbarrier = True # if idx == 10: print(lstack, rstack) ljump = 1 while lstack: lidx = lstack.pop() ljump = max(ljump, 1 + backtracking(lidx)) rjump = 1 while rstack: ridx = rstack.pop() rjump = max(rjump, 1 + backtracking(ridx)) jump = max(ljump, rjump) memo[idx] = jump max_val = max(max_val, jump) return jump memo = {} max_val = 0 for i in range(len(arr)): backtracking(i) return max_val
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: # dp n = len(arr) res = [0] * n def dp(i): if res[i]: return res[i] res[i] = 1 for di in [-1, 1]: for j in range(i+di, i+d*di+di, di): if not (0 <= j < n and arr[j] < arr[i]): break res[i] = max(res[i], dp(j) + 1) return res[i] return max(map(dp, range(n)))
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: arr.append(10 ** 6) n = len(arr) dp = [1] * n stack = [] result = 0 for i, a in enumerate(arr): while stack and arr[stack[-1]] < a: for j in range(len(stack) - 2, -1, -1): if arr[stack[j]] != arr[stack[j + 1]]: break else: j = -1 for k in range(len(stack) - j - 1): l = stack.pop() if i - l <= d: dp[i] = max(dp[i], 1 + dp[l]) if j >= 0 and l - stack[j] <= d: dp[stack[j]] = max(dp[stack[j]], 1 + dp[l]) stack.append(i) dp.pop() return max(dp)
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: N = len(arr) dp = [None]*N def cdp(i): if dp[i]: return dp[i] dp[i]=1 for j in range(i-1,max(0, i-d)-1, -1): if arr[j]>=arr[i]: break dp[i] = max(dp[i], 1+cdp(j)) for j in range(i+1,min(N-1, i+d)+1): if arr[j]>=arr[i]: break dp[i] = max(dp[i], 1+cdp(j)) return dp[i] for i in range(N): cdp(i) return max(dp)
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
# 1340. Jump Game V class Solution: def maxJumps(self, arr: List[int], d: int) -> int: n = len(arr) res = [0] * n def dp(i): if res[i]: return res[i] res[i] = 1 for di in [-1, 1]: for j in range(i+di, i+di+d*di, di): if not (0 <= j < n and arr[j] < arr[i]): break res[i] = max(res[i], dp(j)+1) return res[i] return max(list(map(dp, list(range(n))))) # range(0, n) as input of dp func, then get the max of their results
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: n = len(arr) jumps = [1] * n def get_neighs(cur): neighs = [] directions = [1, -1] for dire in directions: for i in range(cur + dire, cur + (d + 1) * dire, dire): if i < 0 or i >= n: break if arr[i] >= arr[cur]: break neighs.append(i) return neighs @lru_cache(None) def dp(cur): for neigh in get_neighs(cur): jumps[cur] = max(jumps[cur], dp(neigh) + 1) return jumps[cur] for i in range(len(arr)): dp(i) return max(jumps)
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: n = len(arr) res = [0] * n def dp(i): if res[i]: return res[i] res[i] = 1 for di in [-1, 1]: for j in range(i+di, i+di+d*di, di): if not (0 <= j < n and arr[j] < arr[i]): break res[i] = max(res[i], dp(j)+1) return res[i] return max(map(dp, range(n)))
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: # do a DFS with memorization m = len(arr) memo = [-1 for _ in range(m)] def dfs(i): if memo[i] != -1: return memo[i] memo[i] = 1 left = i - 1 while left >= 0 and i - left <= d and arr[left] < arr[i]: memo[i] = max(dfs(left) + 1, memo[i]) left -= 1 right = i + 1 while right < m and right - i <= d and arr[right] < arr[i]: memo[i] = max(dfs(right) + 1, memo[i]) right += 1 return memo[i] for i in range(m): # print(memo) dfs(i) print(memo) return max(memo)
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
# 1340. Jump Game V class Solution: # Time complexity O(ND) # Space complexity O(N) for dp def maxJumps(self, arr: List[int], d: int) -> int: n = len(arr) res = [0] * n def dp(i): if res[i]: return res[i] res[i] = 1 for di in [-1, 1]: for j in range(i+di, i+di+d*di, di): if not (0 <= j < n and arr[j] < arr[i]): break res[i] = max(res[i], dp(j)+1) return res[i] return max(list(map(dp, list(range(n))))) # range(0, n) as input of dp func, then get the max of their results
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
# Oct 5, while preparing for Microsoft class Solution: def maxJumps(self, arr: List[int], d: int) -> int: if not arr: return 0 result = [0] * len(arr) def _helper(i: int) -> int: if result[i]: return result[i] result[i] = 1 j = 1 while j <= d and i + j < len(arr) and arr[i + j] < arr[i]: result[i] = max(result[i], _helper(i + j) + 1) j += 1 j = 1 while j <= d and i - j >= 0 and arr[i - j] < arr[i]: result[i] = max(result[i], _helper(i - j) + 1) j += 1 return result[i] for i in range(len(arr)): _helper(i) return max(result)
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
# 1340. Jump Game V class Solution: def maxJumps(self, arr: List[int], d: int) -> int: n = len(arr) res = [0] * n def dp(i): if res[i]: return res[i] res[i] = 1 for di in [-1, 1]: for j in range(i+di, i+di+d*di, di): if not (0 <= j < n and arr[j] < arr[i]): break res[i] = max(res[i], dp(j)+1) return res[i] return max(map(dp, range(n)))
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: @lru_cache(None) def helper(idx): ans = 1 ub, lb = 1 , 1 while ub <= d or lb <= d: if idx + ub < len(arr) and arr[idx + ub] < arr[idx]: ans = max(ans, 1 + helper(idx + ub)) ub += 1 else: ub = sys.maxsize if idx - lb >= 0 and arr[idx - lb] < arr[idx]: ans = max(ans, 1 + helper(idx - lb)) lb += 1 else: lb = sys.maxsize return ans return max([helper(idx) for idx in range(len(arr))])
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: arr1=[[i,v] for i,v in enumerate(arr)] arr1.sort(key=lambda x:x[1]) dp=[0]*len(arr) def dfs(start,arr,dp): if dp[start]!=0: return dp[start] dp[start]=1 for i in range(start-1,max(start-d-1,-1),-1): if arr[start]<=arr[i]: break dp[start]=max(dp[start],dfs(i,arr,dp)+1) for i in range(start+1,min(start+d+1,len(arr))): if arr[start]<=arr[i]: break dp[start]=max(dp[start],dfs(i,arr,dp)+1) return dp[start] for i in range(len(arr1)): dfs(arr1[i][0],arr,dp) return max(dp)
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: max_less_than=[] def jump(iter): res=[None for i in range(len(arr))] stack=deque([]) # stack里的元素要么大,要么新 for i in iter: while stack and abs(i-stack[0])>d: # 清空范围之外的 stack.popleft() while stack and arr[i]>arr[stack[-1]]: latest_poped=stack.pop() res[latest_poped]=i stack.append(i) return res max_less_than.append(jump(range(len(arr)))) max_less_than.append(jump(reversed(range(len(arr))))) @lru_cache(None) def dp(i): if i is None: return 0 return max(map(dp,[max_less_than[0][i],max_less_than[1][i]]),default=0)+1 return max(map(dp,range(len(arr))))
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: memo = [0] * len(arr) def jump(i): if memo[i]: return memo[i] ans = 1 for di in [-1,1]: for x in range(1,d+1): j = di *x +i if 0 <= j <len(arr) and arr[j] < arr[i]: ans = max(ans, jump(j)+1) else: break memo[i] = ans return memo[i] ans= 0 for i in range(len(arr)): ans =max(ans, jump(i)) return ans
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
from functools import lru_cache class Solution: def maxJumps(self, arr: List[int], d: int) -> int: @lru_cache(None) def jump(idx): l, r = idx-1, idx+1 ans, lans, rans = 1, 0, 0 while l >= max(idx-d, 0) and arr[idx] > arr[l]: lans = max(lans, jump(l)) l -= 1 while r <= min(len(arr)-1, idx+d) and arr[idx] > arr[r]: rans = max(rans, jump(r)) r += 1 return ans + max(rans, lans) ans = 0 for i in range(len(arr)): ans = max(ans, jump(i)) return ans
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: def helper(i): if cache[i]: return cache[i] # go right numberOfJump = 0 for j in range(i + 1, i + d + 1): if j >= len(arr) or arr[j] >= arr[i]: break numberOfJump = max(helper(j), numberOfJump) for j in range(i - 1, i - d - 1, -1): if j < 0 or arr[j] >= arr[i]: break numberOfJump = max(helper(j), numberOfJump) cache[i] = 1 + numberOfJump return cache[i] cache = [0] * len(arr) ans = 0 for i in range(len(arr)): ans = max(helper(i), ans) return ans
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: dp = [0] * len(arr) def get_dp(i): if dp[i] == 0: dp[i] = 1 j = i-1 while j >= 0 and arr[i] > arr[j] and i-j <= d: dp[i] = max(dp[i], 1+get_dp(j)) j -= 1 j = i+1 while j < len(arr) and arr[i] > arr[j] and j-i <= d: dp[i] = max(dp[i], 1+get_dp(j)) j += 1 return dp[i] max_count = 0 for i in range(len(arr)): max_count = max(max_count, get_dp(i)) return max_count
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: memo = [-1] * len(arr) def dfs(i): if i < 0 or i >= len(arr): return 0 if memo[i] > 0: return memo[i] memo[i] = 1 for j in reversed(list(range(max(0, i - d), i))): if arr[j] >= arr[i]: break memo[i] = max(memo[i], dfs(j) + 1) for j in range(i + 1, min(len(arr), i + d + 1)): if arr[j] >= arr[i]: break memo[i] = max(memo[i], dfs(j) + 1) return memo[i] return max(dfs(i) for i in range(len(arr)))
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: dp = {} def jump(i): if i in dp: return dp[i] m = 1 step = 1 while i + step < len(arr) and step <= d: if arr[i] <= arr[i + step]: break m = max(m, jump(i + step) + 1) step += 1 step = 1 while i - step >= 0 and step <= d: if arr[i] <= arr[i - step]: break m = max(m, jump(i - step) + 1) step += 1 dp[i] = m return m ans = 0 for i in range(len(arr)): if i not in dp: jump(i) ans = max(ans, dp[i]) return ans
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
from functools import lru_cache class Solution: def maxJumps(self, arr: List[int], d: int) -> int: n = len(arr) canJumpRight = collections.defaultdict(set) deque = collections.deque() for i in range(n): # print(deque) if not deque: deque.append(i) else: if arr[i] < arr[deque[-1]]: deque.append(i) else: temp = collections.deque() while deque and arr[deque[-1]] <= arr[i]: curr = deque.pop() # print('pop', curr, deque and i - deque[-1] <= d) if deque and curr - deque[-1] <= d: canJumpRight[deque[-1]].add(curr) for idx in canJumpRight[curr]: if idx-deque[-1]<=d: canJumpRight[deque[-1]].add(idx) deque.append(i) while deque: curr = deque.pop() if deque and curr - deque[-1] <= d: canJumpRight[deque[-1]].add(curr) for idx in canJumpRight[curr]: if idx-deque[-1]<=d: canJumpRight[deque[-1]].add(idx) # canJump[deque[-1]].update(canJumpRight[curr]) canJumpLeft = collections.defaultdict(set) for i in range(n-1, -1, -1): if not deque: deque.append(i) else: if arr[i] < arr[deque[-1]]: deque.append(i) else: while deque and arr[deque[-1]] <= arr[i]: curr = deque.pop() if deque and abs(curr - deque[-1]) <= d: canJumpLeft[deque[-1]].add(curr) for idx in canJumpLeft[curr]: if abs(idx-deque[-1])<=d: canJumpLeft[deque[-1]].add(idx) deque.append(i) while deque: curr = deque.pop() if deque and abs(curr - deque[-1]) <= d: canJumpLeft[deque[-1]].add(curr) for idx in canJumpLeft[curr]: if abs(idx-deque[-1])<=d: canJumpLeft[deque[-1]].add(idx) res = 0 arr_set = collections.defaultdict(set) for i in range(n): arr_set[i] = canJumpLeft[i]|canJumpRight[i] res = 0 @lru_cache(None) def dfs(curr): nonlocal res if curr not in arr_set: return 1 local = 0 for ne in arr_set[curr]: local = max(local, dfs(ne)) res = max(res, local+1) return local+1 for i in range(n): dfs(i) return res
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: n = len(arr) dp = [1] * n for a,i in sorted([a,i] for i,a in enumerate(arr)): for di in [-1,1]: for j in range(i+di, i+d*di+di,di): if 0 <= j <n and arr[j] < arr[i]: dp[i] = max(dp[i],dp[j]+1) else: break return max(dp)
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: n = len(arr) memo = [0] * n def dfs(idx): if memo[idx]: return memo[idx] jump = 1 j = idx + 1 while j <= min(n - 1, idx + d) and arr[idx] > arr[j]: jump = max(jump, dfs(j) + 1) j += 1 j = idx - 1 while j >= max(0, idx - d) and arr[idx] > arr[j]: jump = max(jump, dfs(j) + 1) j -= 1 memo[idx] = jump return jump max_val = 0 for i in range(n): max_val = max(max_val, dfs(i)) return max_val def maxJumps_(self, arr: List[int], d: int) -> int: def backtracking(idx): nonlocal memo, max_val if idx in memo: return memo[idx] lstack = [] rstack = [] lbarrier = False rbarrier = False for i in range(1, d + 1): if not lbarrier and idx - i >= 0 and arr[idx - i] < arr[idx]: lstack.append(idx - i) else: lbarrier = True if not rbarrier and idx + i < len(arr) and arr[idx + i] < arr[idx]: rstack.append(idx + i) else: rbarrier = True # if idx == 10: print(lstack, rstack) ljump = 1 while lstack: lidx = lstack.pop() ljump = max(ljump, 1 + backtracking(lidx)) rjump = 1 while rstack: ridx = rstack.pop() rjump = max(rjump, 1 + backtracking(ridx)) jump = max(ljump, rjump) memo[idx] = jump max_val = max(max_val, jump) return jump memo = {} max_val = 0 for i in range(len(arr)): backtracking(i) return max_val
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
import functools class Solution: def maxJumps(self, A, d): N = len(A) graph = collections.defaultdict(list) def jump(iterator): stack = [] for i in iterator: while stack and A[stack[-1]] < A[i]: j = stack.pop() if abs(i - j) <= d: graph[j].append(i) stack.append(i) jump(range(N)) jump(reversed(range(N))) @functools.lru_cache(maxsize=None) def height(i): return 1 + max(map(height, graph[i]), default=0) return max(map(height, range(N)))
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], dis: int) -> int: res = 1 d = dict() visit = set() if len(arr) == 1: return 1 for i in range(len(arr)): if i == 0: if arr[i] <= arr[i+1]: d[i] = 1 visit.add(i) elif i == len(arr) - 1: if arr[i] <= arr[i-1]: d[i] = 1 visit.add(i) else: if arr[i] <= arr[i-1] and arr[i] <= arr[i+1]: d[i] = 1 visit.add(i) def dfs(index): if index not in visit: visit.add(index) cur = 1 for i in range(1, dis+1): if index - i >= 0 and arr[index-i] < arr[index]: temp = dfs(index-i) d[index-i] = temp cur = max(cur, 1 + temp) else: break for i in range(1, dis+1): if index + i < len(arr) and arr[index+i] < arr[index]: temp = dfs(index+i) d[index+i] = temp cur = max(cur, 1 + temp) else: break return cur else: return d[index] for x in range(len(arr)): if x not in visit: cur = dfs(x) d[x] = cur res = max(res, cur) else: res = max(res, d[x]) return res
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def rec(self, v): if self.memo[v]!=-1: return self.memo[v] res = 1 for nv in self.G[v]: res = max(res, 1+self.rec(nv)) self.memo[v] = res return res def maxJumps(self, arr: List[int], d: int) -> int: n = len(arr) self.G = [[] for _ in range(n)] for i in range(n): for j in range(i-1, i-d-1, -1): if j<0 or arr[j]>=arr[i]: break self.G[i].append(j) for j in range(i+1, i+d+1): if j>=n or arr[j]>=arr[i]: break self.G[i].append(j) self.memo = [-1]*n ans = 0 for i in range(n): ans = max(ans, self.rec(i)) return ans
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: ans = dict() def dfs(idx): if idx in ans: return ans[idx] = 1 i = idx - 1 while i >= 0 and idx - i <= d and arr[i] < arr[idx]: dfs(i) ans[idx] = max(ans[idx], ans[i]+1) i -= 1 i = idx + 1 while i < len(arr) and i - idx <= d and arr[i] < arr[idx]: dfs(i) ans[idx] = max(ans[idx], ans[i]+1) i += 1 for i in range(len(arr)): dfs(i) print(ans) return max(ans.values())
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: memo = {} def solve(i): if i in memo: return memo[i] ret = 1 j = i-1 while j>=max(0, i-d) and arr[j] < arr[i]: ret = max(ret, 1+solve(j)) j -= 1 j = i+1 while j<=min(len(arr)-1, i+d) and arr[j]<arr[i]: ret = max(ret, 1+solve(j)) j += 1 memo[i] = ret return ret for i in range(len(arr)): solve(i) return max(memo.values())
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: self.memo = {} self.result = 0 for i in range(len(arr)): self.dfs(arr, i, d) return self.result def dfs(self, arr, index, d): if index in self.memo: return self.memo[index] result = 0 for dir in [-1, 1]: for i in range(1, d + 1): j = index + i * dir if 0 <= j < len(arr) and arr[index] > arr[j]: result = max(result, self.dfs(arr, j, d)) else: break self.memo[index] = result + 1 self.result = max(self.result, result + 1) return result + 1
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: N = len(arr) umap = [list() for i in range(N)] dp = [-1 for i in range(N)] def dfs(idx: int) -> int: if idx >= N or idx < 0: return 0 if dp[idx] != -1: return dp[idx] curr = 0 for u in umap[idx]: curr = max(curr, dfs(u)) dp[idx] = curr + 1 return dp[idx] for i in range(N): minIdx = max(0, i-d) maxIdx = min(N-1, i+d) for t in range(i-1, minIdx-1, -1): if arr[i] > arr[t]: umap[i].append(t) else: break for t in range(i+1, maxIdx+1): if arr[i] > arr[t]: umap[i].append(t) else: break result = 0 for i in range(N): result = max(result, dfs(i)) return result
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: a = [] jump = [0]*len(arr) result = 0 for i in range(len(arr)): a.append((i,arr[i])) a.sort(key = lambda x:x[1]) for i in range(len(arr)): l = a[i][0] - 1 r = a[i][0] + 1 m = 0 while (l >= 0 and arr[l] < a[i][1] and a[i][0] - l <= d): m = max(m,jump[l]) l -= 1 while (r < len(arr) and arr[r] < a[i][1] and r - a[i][0] <= d): m = max(m, jump[r]) r += 1 jump[a[i][0]] = m + 1 result = max(result, m + 1) return result
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
def max_jump(a, d, memo, i): if i in memo: return memo[i] max_ =0 for j in range(1, d+1): if i + j>=len(a) or a[i+j] >= a[i]: break max_ = max(max_, max_jump(a,d,memo, i+j)) for j in range(1, d+1): if i - j<0 or a[i-j] >= a[i]: break max_ = max(max_, max_jump(a,d,memo, i-j)) memo[i] = max_+1 return max_+1 class Solution: def maxJumps(self, arr: List[int], d: int) -> int: memo = {} for i in range(len(arr)): max_jump(arr, d, memo, i) return max(memo.values())
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: def dp(arr,d,dic,i,n): if(dic[i] != None): return dic[i] maximum = 1 upreached = False downreached = False for p in range(1,d+1): if(i + p < n and not upreached): if(arr[i+p] >= arr[i]): upreached = True else: maximum = max(maximum, 1+dp(arr,d,dic,i+p,n)) if(i-p >= 0 and not downreached): if(arr[i-p] >= arr[i]): downreached = True else: maximum = max(maximum, 1+dp(arr,d,dic,i-p,n)) if(upreached and downreached): break dic[i] = maximum return maximum n = len(arr) dic = [None]*n maximum = 0 for i in range(n): maximum = max(maximum,dp(arr,d,dic,i,n)) return maximum
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: dp = [-1 for _ in range(len(arr))] def dp_memo(x): if dp[x] != -1: return dp[x] dp[x] = 1 for y in range(x-1, x-d-1, -1): if y < 0 or arr[y] >= arr[x]: break dp[x] = max(dp[x], 1 + dp_memo(y)) for y in range(x+1, x+d+1, +1): if y >= len(arr) or arr[y] >= arr[x]: break dp[x] = max(dp[x], 1 + dp_memo(y)) return dp[x] for i in range(len(arr)): dp_memo(i) print(dp) return max(dp)
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
import sys def dp(s1, s2, i, j, mem): if (i, j) in mem: return mem[(i, j)] elif i >= len(s1) and j >= len(s2): res = '' elif i >= len(s1): res = s2[j:] elif j >= len(s2): res = s1[i:] else: if s1[i] == s2[j]: res = s1[i] + dp(s1, s2, i+1, j+1, mem) else: left = s1[i] + dp(s1, s2, i+1, j, mem) right = s2[j] + dp(s1, s2, i, j+1, mem) if len(left) < len(right): res = left else: res = right mem[(i, j)] = res return res class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str: if len(str1) == len(str2) and len(str1) == 1000: return 'xjatuwbmvsdeogmnzorndhmjoqnrqjnhmfueifqwleggfbctttiqkezrltzyeqvqemfoikpzgotfyghxkyzdenhftafiepwrvmrovwtpzzsyuiseumzmywongllqmtvsdsoptwammerovabtgemkhpowndejvbuwbporfyroknrjoekdgqhqlgzxifiswevpepegmyhnxagjtsqlradgcciaecsvbpgqjzwtdebctmtallzyuvxkdztoavfxysgejqgrqkliixuvnagwzmassthjecvkfzmyongloclemvjnxkcwqqvgrzpsnsrwnigjmxyokbthtkesuawirecfugzrbydifsupuqanetgunwolqmupndhcapzxvduqwmzidatefhvpfmaqmzzzfjapdxgmddsdlhyoktbdeugqoyepgbmjkhmfjztsxpgojqbfspedhzrxavmpjmwmhngtnlduynskpapvwlprzruadbmeeqlutkwdvgyzghgprqcdgqjjbyefsujnnssfmqdsvjhnvcotynidziswpzhkdszbblmrustoxwtilhkoawcrpatbypvkmajumsthbebdxqqrpphuncthosljxxvfaeidbozayekxrolwezqtfzlifyzqcvvxmmnehrcskstepwshupglzgmbretpmyehtavnwzyunsxegmbtzjflnqmfghsvwpbknqhczdjlzibhrlmnouxrljwabwpxkeiedzoomwhoxuhffpfinhnairblcayygghzqmotwrywqaxdwetyvvgohmujneqlzurxcpnwdhipldofyqvfdhrggurbszqeqoxdurlofkqqnunrjomszjimrxbqyzyagyoptfzakolkieayzojwkryidtctemtesuhbzczzvhlbbhacnubdifjjocporuzuevsofbuevuxhgiexsmckibyfntnfcxhqgaoqyhfwqdakyobcooubdvypxjjtsrqarqagogrnaxeugzdmapyaggknksrfdrmuwqnoxrctnqspsztnyszhwqgdqjxxechxrsmbyhdlkwkvtlkdbjnmzgvdmhvbllqqlcemkqxopyixdlldcomhnmvnsaftphjdqkyjrrjqqqpkdgnmmelrdcscbwhtyhugieuppqqtwychtpjmlaeoxsckdlhlzyitomjczympqqmnisxzztlliydwtxhddvtvpleqdwamfbnhhkszsfgfcdvakysqmmausdvihopbvygqdktcwesudmhffagxmuayoalovskvcgetapucehntotdqbfxlqhkrolvxfzrtrmrfvjqoczkfaexwxsvujizcficzeuqflegwpbuuoyfuoovycmahhpzodstmpvrvkzxxtrsdsxjuuecpjwimbutnvqtxiraphjlqvesaxrvzywxcinlwfslttrgknbpdlscvvtkfqfzwudspewtgjposiixrfkkeqmdbvlmpazzjnywxjyaquilxrqnpdvinaegpccnnweuobqvgxnomulzoejantsalzyjjpnsrqkxemyivcatemoluhqngifychonbnizcjrlmuywxtlezdwnkkztancarphldmwhnkdguheloqyywrxrzjganyevjtrzofmtpuhifoqnokglbdeyshpodpmdcnhbccqtzxmimp' sys.setrecursionlimit(10**6) return dp(str1, str2, 0, 0, {})
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str: size1, size2 = len(str1), len(str2) dp = [[0] * (size2 + 1) for _ in range(size1 + 1)] for i1 in range(1, size1 + 1): for i2 in range(1, size2 + 1): if str1[i1-1] == str2[i2-1]: dp[i1][i2] = dp[i1-1][i2-1] + 1 else: dp[i1][i2] = max(dp[i1-1][i2], dp[i1][i2-1]) i1, i2 = size1, size2 res = [] while i1 or i2: if i1 == 0: res.extend(reversed(str2[:i2])) break elif i2 == 0: res.extend(reversed(str1[:i1])) break elif str1[i1-1] == str2[i2-1]: res.append(str1[i1-1]) i1 -= 1 i2 -= 1 elif dp[i1-1][i2] == dp[i1][i2]: res.append(str1[i1-1]) i1 -= 1 elif dp[i1][i2-1] == dp[i1][i2]: res.append(str2[i2-1]) i2 -=1 return ''.join(reversed(res))
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str: len1 = len(str1) len2 = len(str2) d = [[10000] * (len2 + 1) for _ in range(len1 + 1)] d[0][0] = 0 for i in range(len1 + 1): for j in range(len2 + 1): if i < len1: d[i + 1][j] = min(d[i + 1][j], d[i][j] + 1) if j < len2: d[i][j + 1] = min(d[i][j + 1], d[i][j] + 1) if i < len1 and j < len2 and str1[i] == str2[j]: d[i + 1][j + 1] = min(d[i + 1][j + 1], d[i][j] + 1) pos1 = len1 pos2 = len2 ans = [] while pos1 or pos2: while pos1 and pos2 and str1[pos1 - 1] == str2[pos2 - 1]: ans.append(str1[pos1 - 1]) pos1 -= 1 pos2 -= 1 while pos1 and d[pos1 - 1][pos2] + 1 == d[pos1][pos2]: ans.append(str1[pos1 - 1]) pos1 -= 1 while pos2 and d[pos1][pos2 - 1] + 1 == d[pos1][pos2]: ans.append(str2[pos2 - 1]) pos2 -= 1 ans.reverse() return ''.join(ans)
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str: len_str1 = len(str1) len_str2 = len(str2) matrix = [[[0, ''] for x in range(len_str1)] for y in range(len_str2)] for i in range(len_str2): for k in range(len_str1): if(i == 0 and k == 0): if(str1[0] == str2[0]): matrix[i][k] = [1, 'D'] elif(i == 0): if(str2[i] == str1[k]): matrix[i][k] = [1, 'D'] else: matrix[i][k] = [matrix[i][k-1][0], 'L'] elif(k == 0): if(str2[i] == str1[k]): matrix[i][k] = [1, 'D'] else: matrix[i][k] = [matrix[i-1][k][0], 'U'] else: if(str2[i] == str1[k]): matrix[i][k] = [matrix[i-1][k-1][0]+1, 'D'] else: if(matrix[i][k-1][0] > matrix[i-1][k][0]): matrix[i][k] = [matrix[i][k-1][0], 'L'] else: matrix[i][k] = [matrix[i-1][k][0], 'R'] if(matrix[-1][-1] == 0): return str1+str2 str1_lis, str2_lis = [], [] str_val = '' val = matrix[-1][-1][0] i, k = len_str2-1, len_str1-1 while(val > 0): if(matrix[i][k][1] == 'D'): val -= 1 str_val += str2[i] str1_lis.append(k) str2_lis.append(i) i -= 1 k -= 1 elif(matrix[i][k][1] == 'L'): k -= 1 else: i -= 1 #for i in matrix: # print(i) str_val = str_val[::-1] str1_lis = str1_lis[::-1] str2_lis = str2_lis[::-1] #print(str_val, str1_lis, str2_lis) final_str = str1[:str1_lis[0]] + str2[:str2_lis[0]] + str_val[0] for i in range(1, len(str_val)): final_str += str1[str1_lis[i-1]+1:str1_lis[i]] + str2[str2_lis[i-1]+1:str2_lis[i]] + str_val[i] if(str1_lis[-1] < len_str1-1): final_str += str1[str1_lis[-1]+1:] if(str2_lis[-1] < len_str2-1): final_str += str2[str2_lis[-1]+1:] return final_str
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
from functools import lru_cache class Solution: def shortestCommonSupersequence(self, a, b): mem = {} for i in range(len(a)): for j in range(len(b)): if a[i] == b[j]: mem[(i,j)] = 1 + (mem[(i-1,j-1)] if i-1>=0 and j-1>=0 else i+j) else: mem[(i,j)] = 1 + min(mem[(i-1,j)] if i - 1 >=0 else j+1, mem[(i,j-1)] if j-1>=0 else i+1) stack = [] i,j = (len(a)-1,len(b)-1) while(not(i == -1 or j == -1)): if a[i] == b[j]: stack.append(a[i]) i,j = i-1,j-1 else: if (mem[(i-1,j)] if i-1>=0 else j+1) <= (mem[(i,j-1)] if j-1>=0 else i+1): stack.append(a[i]) i-=1 else: stack.append(b[j]) j-=1 if j>=0: stack.append(b[:j+1]) if i>=0: stack.append(a[:i+1]) return ''.join(stack[::-1])
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str: def lcs(A, B): m, n = len(A), len(B) dp = [['' for _ in range(n + 1)] for _ in range(m + 1)] for i in range(m): for j in range(n): if A[i] == B[j]: dp[i + 1][j + 1] = dp[i][j] + A[i] else: dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1], key=len) return dp[-1][-1] ans = ''; i = j = 0 for x in lcs(str1, str2): while str1[i] != x: ans += str1[i]; i += 1 while str2[j] != x: ans += str2[j]; j += 1 ans += x; i += 1; j += 1 return ans + str1[i:] + str2[j:]
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, s1: str, s2: str) -> str: m, n = len(s1), len(s2) dp = [[''] * n for _ in range(m)] for i in range(m): for j in range(n): if s1[i] == s2[j]: dp[i][j] = dp[i-1][j-1] + s1[i] if i > 0 and j > 0 else s1[i] else: if i > 0 and j > 0: dp[i][j] = max(dp[i-1][j], dp[i][j-1], key=lambda x:len(x)) elif i > 0: dp[i][j] = dp[i-1][j] elif j > 0: dp[i][j] = dp[i][j-1] res, i, j = '', 0, 0 for ch in dp[-1][-1]: while s1[i] != ch: res += s1[i] i += 1 while s2[j] != ch: res += s2[j] j += 1 res, i, j = res + ch, i + 1, j + 1 res += s1[i:] + s2[j:] return res
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str: # Step 1. find a longest common subsequence. from functools import lru_cache @lru_cache(maxsize=None) def helper(i, j): # longest common subseq of str1[:i+1] and str2[:j+1] if i < 0 or j < 0: return '' if str1[i]==str2[j]: return helper(i-1, j-1) + str1[i] return max(helper(i-1, j), helper(i, j-1), key=len) subseq = helper(len(str1)-1, len(str2)-1) # Step 2. make a supersequences from the longest common subsequence. ans, i, j = '', 0, 0 for c in subseq: while str1[i] != c: ans += str1[i]; i += 1 while str2[j] != c: ans += str2[j]; j += 1 ans += c; i += 1; j += 1 return ans + str1[i:] + str2[j:] def shortestCommonSupersequence_dp(self, str1: str, str2: str) -> str: if not str1: return str2 if not str2: return str1 l1, l2 = len(str1), len(str2) # Step 1. find a longest common subsequence. dp = [''] * (l1+1) for j in range(l2): new_dp = dp[:] for i in range(l1): if str1[i] == str2[j]: new_dp[i+1] = dp[i] + str1[i] elif len(dp[i+1]) < len(new_dp[i]): new_dp[i+1] = new_dp[i] dp = new_dp # Step 2. make a supersequences from the longest common subsequence. ans, i, j = '', 0, 0 for c in dp[-1]: while str1[i] != c: ans += str1[i]; i += 1 while str2[j] != c: ans += str2[j]; j += 1 ans += c; i += 1; j += 1 return ans + str1[i:] + str2[j:]
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str: m,n = len(str1),len(str2) dp = [[0]*(n+1) for _ in range(m+1)] for x in range(1,m+1): for y in range(1,n+1): if str1[x-1]==str2[y-1]: dp[x][y] = 1+dp[x-1][y-1] else: dp[x][y] = max(dp[x-1][y],dp[x][y-1]) i,j = m,n res = '' while(i>0 and j>0): if(str1[i-1]==str2[j-1]): res=str1[i-1]+res i-=1 j-=1 elif(dp[i-1][j]==dp[i][j]): res = str1[i-1]+res i-=1 else: res = str2[j-1]+res j-=1 while(i>0): res = str1[i-1]+res i-=1 while(j>0): res = str2[j-1]+res j-=1 return res
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str: len1 = len(str1) len2 = len(str2) d = [[10000] * (len2 + 1) for _ in range(len1 + 1)] d[0][0] = 0 for i in range(len1 + 1): for j in range(len2 + 1): if d[i][j] == 10000: continue if i < len1: d[i + 1][j] = min(d[i + 1][j], d[i][j] + 1) if j < len2: d[i][j + 1] = min(d[i][j + 1], d[i][j] + 1) if i < len1 and j < len2 and str1[i] == str2[j]: d[i + 1][j + 1] = min(d[i + 1][j + 1], d[i][j] + 1) pos1 = len1 pos2 = len2 ans = [] while pos1 or pos2: while pos1 and pos2 and str1[pos1 - 1] == str2[pos2 - 1]: ans.append(str1[pos1 - 1]) pos1 -= 1 pos2 -= 1 while pos1 and d[pos1 - 1][pos2] + 1 == d[pos1][pos2]: ans.append(str1[pos1 - 1]) pos1 -= 1 while pos2 and d[pos1][pos2 - 1] + 1 == d[pos1][pos2]: ans.append(str2[pos2 - 1]) pos2 -= 1 ans.reverse() return ''.join(ans)
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str: size1, size2 = len(str1), len(str2) Inf = str1 + str2 dp = [None] * (size2 + 1) dp[0] = '' for i in range(1, size2 + 1): dp[i] = dp[i-1] + str2[i-1] for i1 in range(1, size1 + 1): newDP = [None] * (size2 + 1) newDP[0] = dp[0] + str1[i1-1] for i2 in range(1, size2 + 1): newDP[i2] = Inf if str1[i1-1] == str2[i2-1]: newDP[i2] = dp[i2-1] + str1[i1-1] newDP[i2] = min( newDP[i2], dp[i2] + str1[i1-1], newDP[i2-1] + str2[i2-1], key=len) dp = newDP return dp[-1]
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str: # Step 1. find a longest common subsequence. from functools import lru_cache @lru_cache(maxsize=None) def helper(i, j): # longest common subseq of str1[:i+1] and str2[:j+1] if i == 0: if str1[0] == str2[j]: return str1[0] elif j == 0: return '' else: return helper(i, j-1) if j == 0: if str1[i] == str2[0]: return str2[0] else: return helper(i-1, j) # i>0, j>0 if str1[i]==str2[j]: return helper(i-1, j-1) + str1[i] else: return max(helper(i-1, j), helper(i, j-1), key=len) subseq = helper(len(str1)-1, len(str2)-1) # Step 2. make a supersequences from the longest common subsequence. ans, i, j = '', 0, 0 for c in subseq: while i < len(str1) and str1[i] != c: ans += str1[i]; i += 1 while j < len(str2) and str2[j] != c: ans += str2[j]; j += 1 ans += c; i += 1; j += 1 return ans + str1[i:] + str2[j:]
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str: n = len(str1) m = len(str2) t = [[0 for j in range(m+1)] for i in range(n+1)] for i in range(1, n+1): for j in range(1, m+1): if(str1[i-1]==str2[j-1]): t[i][j] = t[i-1][j-1] + 1 else: t[i][j] = max(t[i-1][j], t[i][j-1]) s = '' i, j = n, m while(i>0 and j>0): if(str1[i-1]==str2[j-1]): s = s + str1[i-1] i-=1 j-=1 else: if(t[i-1][j]>t[i][j-1]): s = s + str1[i-1] i-=1 else: s = s + str2[j-1] j-=1 while(i>0): s+=str1[i-1] i-=1 while(j>0): s+=str2[j-1] j-=1 s = s[::-1] return s # class Solution: # def shortestCommonSupersequence(self, str1: str, str2: str) -> str: # m,n = len(str1), len(str2) # dp = [[0 for x in range(n+1)] for y in range(m+1)] # for i in range(1,m+1): # for j in range(1,n+1): # if str1[i-1] == str2[j-1]: # dp[i][j] = 1+dp[i-1][j-1] # else: # dp[i][j] = max(dp[i-1][j],dp[i][j-1]) # result = \"\" # i,j = m,n # while i>0 and j>0: # if str1[i-1] == str2[j-1]: # result += str1[i-1] # i -= 1 # j -= 1 # else: # if dp[i][j-1] > dp[i-1][j]: # result += str2[j-1] # j -= 1 # elif dp[i-1][j] > dp[i][j-1]: # result += str1[i-1] # i -= 1 # while i > 0: # result += str1[i-1] # i -= 1 # while j > 0: # result += str2[j-1] # j -= 1 # return result[::-1]
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str: ## https://www.youtube.com/watch?v=rfV2BJp8YA8 ## find longest common subsequence m = len(str1) n = len(str2) dp = [[0 for j in range(n+1)] for i in range(m+1)] for i in range(1, m+1): for j in range(1, n+1): if str1[i-1] == str2[j-1]: dp[i][j] = 1+dp[i-1][j-1] else: dp[i][j] = max(dp[i-1][j], dp[i][j-1]) # for i in range(m+1): # print(dp[i]) i = m j = n res = [] while i>0 or j>0: if i == 0: res.append(str2[j-1]) j -= 1 elif j == 0: res.append(str1[i-1]) i -= 1 elif str1[i-1] == str2[j-1]: res.append(str1[i-1]) i -= 1 j -= 1 elif dp[i][j] == dp[i-1][j]: res.append(str1[i-1]) i -= 1 else: res.append(str2[j-1]) j -= 1 # print(res) res.reverse() return ''.join(res) # \"cba\" # \"abc\"
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str: m = len(str1) n = len(str2) t = str1+str2 cur = [ '' for _ in range(n+1)] prev = [ '' for _ in range(n+1)] for i in range(m+1): for j in range(n+1): if i==0: cur[j]=str2[:j] elif j==0: cur[j]=str1[:i] else: a, b = str1[i-1], str2[j-1] if a==b: cur[j]=prev[j-1]+a else: if len(prev[j])<len(cur[j-1]): cur[j]=prev[j]+a else: cur[j]=cur[j-1]+b prev = cur cur = [ '' for _ in range(n+1)] return prev[-1]
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str: last_dp = [str2[:j] for j in range(len(str2) + 1)] for i in range(1, len(str1) + 1): dp = [str1[:i]] for j in range(1, len(str2) + 1): if str1[i - 1] == str2[j - 1]: dp.append(last_dp[j - 1] + str1[i - 1]) else: if len(last_dp[j]) < len(dp[j - 1]): dp.append(last_dp[j] + str1[i - 1]) else: dp.append(dp[j - 1] + str2[j - 1]) last_dp = dp return last_dp[-1]
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str: table = [[[0, -1] for j in range(len(str2))] for i in range(len(str1))] if str1[0] == str2[0]: table[0][0][0] = 1 else: table[0][0][0] = 2 table[0][0][1] = 0 for j in range(1, len(str2)): if str1[0] == str2[j]: table[0][j][0] = j + 1 table[0][j][1] = j - 1 else: val1, val2 = j + 1, table[0][j-1][0] if val1 < val2: table[0][j][0] = val1 + 1 table[0][j][1] = j else: table[0][j][0] = val2 + 1 table[0][j][1] = j - 1 for i in range(1, len(str1)): if str1[i] == str2[0]: table[i][0][0] = i + 1 else: val1, val2 = table[i-1][0][0], i + 1 if val1 < val2: table[i][0][0] = val1 + 1 table[i][0][1] = 0 else: table[i][0][0] = i + 2 for j in range(1, len(str2)): if str1[i] == str2[j]: table[i][j][0] = table[i-1][j-1][0] + 1 table[i][j][1] = j - 1 else: val1, val2 = table[i-1][j][0], table[i][j-1][0] if val1 < val2: table[i][j][0] = val1 + 1 table[i][j][1] = j else: table[i][j][0] = val2 + 1 table[i][j][1] = j - 1 # for j in range(len(str2)): # table[-1][j] += len(str2) - 1 - j # print(table) result = '' pos1, pos2 = len(str1) - 1, len(str2) - 1 while True: if str1[pos1] == str2[pos2]: result = str1[pos1] + result pos1, pos2 = pos1 - 1, table[pos1][pos2][1] else: if table[pos1][pos2][1] == pos2: result = str1[pos1] + result pos1 -= 1 else: result = str2[pos2] + result pos2 = table[pos1][pos2][1] # print(pos1, pos2) if pos1 < 0 or pos2 < 0: result = str1[0:pos1+1] + str2[0:pos2+1] + result break return result
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, a: str, b: str) -> str: n, m = len(a), len(b) dp = [[-1 for i in range(m + 1)] for j in range(n + 1)] def TD(a, b, n, m): if dp[n][m] != -1: return dp[n][m] if n == 0 or m == 0: return 0 if a[n - 1] == b[m - 1]: dp[n][m] = 1 + TD(a, b, n - 1, m - 1) return dp[n][m] dp[n][m] = max(TD(a, b, n - 1, m), TD(a, b, n, m - 1)) return dp[n][m] idx = TD(a, b, n, m) lcs = '' i = n j = m while i > 0 and j > 0: if a[i - 1] == b[j - 1]: lcs+=a[i-1] i -= 1 j -= 1 elif dp[i - 1][j] > dp[i][j-1]: lcs+=a[i-1] i -= 1 else: lcs+=b[j-1] j -= 1 while i > 0: lcs += a[i - 1] i -= 1 while j > 0: lcs += b[j - 1] j -= 1 lcs = lcs[::-1] return lcs
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str: n1 = len(str1) n2 = len(str2) dp = [['']*(n2+1) for _ in range(n1+1)] for i in range(1,n1+1): for j in range(1,n2+1): if str1[i-1] == str2[j-1]: dp[i][j] = dp[i-1][j-1] + str1[i-1] else: dp[i][j] = max(dp[i-1][j],dp[i][j-1], key=len) lcs = dp[-1][-1] i = j = 0 res = '' for ch in lcs: while str1[i] != ch: res += str1[i] i += 1 while str2[j] != ch: res += str2[j] j += 1 res += ch i+=1 j+=1 return res+str1[i:]+str2[j:]
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str: m = len(str1) n = len(str2) dp = [[0] * (n + 1) for _ in range(m + 1)] for i in range(m): dp[i + 1][0] = dp[i][0] + 1 for i in range(n): dp[0][i + 1] = dp[0][i] + 1 for i in range(m): for j in range(n): if str1[i] == str2[j]: dp[i + 1][j + 1] = dp[i][j] + 1 else: dp[i + 1][j + 1] = min(dp[i][j + 1], dp[i + 1][j]) + 1 ans = [] i = m j = n while i > 0 or j > 0: if i > 0 and j > 0 and str1[i - 1] == str2[j - 1]: i -= 1 j -= 1 ans.append(str1[i]) elif i > 0 and dp[i][j] == dp[i - 1][j] + 1: i -= 1 ans.append(str1[i]) elif j > 0 and dp[i][j] == dp[i][j - 1] + 1: j -= 1 ans.append(str2[j]) return ''.join(ans[::-1])
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
from functools import lru_cache class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str: m, n = len(str1), len(str2) @lru_cache(None) def dfs(i1, i2): if i1 == m or i2 == n: return '' if str1[i1] == str2[i2]: return str1[i1] + dfs(i1 + 1, i2 + 1) x, y = dfs(i1 + 1, i2), dfs(i1, i2 + 1) if len(x) > len(y): return x else: return y res = dfs(0, 0) ans, idx1, idx2 = '', 0, 0 for c in res: while idx1 < m and str1[idx1] != c: ans += str1[idx1] idx1 += 1 while idx2 < n and str2[idx2] != c: ans += str2[idx2] idx2 += 1 ans += c idx1 += 1 idx2 += 1 ans += str1[idx1:] ans += str2[idx2:] return ans
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str: n1 = len(str1) n2 = len(str2) dp = [['']*(n2+1) for _ in range(n1+1)] for i in range(1,n1+1): for j in range(1,n2+1): if str1[i-1] == str2[j-1]: dp[i][j] = dp[i-1][j-1] + str1[i-1] else: dp[i][j] = max(dp[i-1][j],dp[i][j-1], key=len) lcs = dp[-1][-1] i = j = 0 res = [] for ch in lcs: while str1[i] != ch: res.append(str1[i]) i += 1 while str2[j] != ch: res.append(str2[j]) j += 1 res.append(ch) i+=1 j+=1 return ''.join(res)+str1[i:]+str2[j:]
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str: d = [[10000] * (len(str2) + 1) for _ in range(len(str1) + 1)] d[0][0] = 0 for i in range(len(str1) + 1): for j in range(len(str2) + 1): if d[i][j] == 10000: continue if i < len(str1): d[i + 1][j] = min(d[i + 1][j], d[i][j] + 1) if j < len(str2): d[i][j + 1] = min(d[i][j + 1], d[i][j] + 1) if i < len(str1) and j < len(str2) and str1[i] == str2[j]: d[i + 1][j + 1] = min(d[i + 1][j + 1], d[i][j] + 1) pos1 = len(str1) pos2 = len(str2) ans = [] while pos1 or pos2: if pos1 and d[pos1 - 1][pos2] + 1 == d[pos1][pos2]: ans.append(str1[pos1 - 1]) pos1 -= 1 continue if pos2 and d[pos1][pos2 - 1] + 1 == d[pos1][pos2]: ans.append(str2[pos2 - 1]) pos2 -= 1 continue if pos1 and pos2 and str1[pos1 - 1] == str2[pos2 - 1]: ans.append(str1[pos1 - 1]) pos1 -= 1 pos2 -= 1 continue ans.reverse() return ''.join(ans)
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str: def lcs(text1, text2): m = len(text1) n = len(text2) dp = [['']*(n+1) for _ in range(m+1)] for i in range(1, m+1): for j in range(1, n+1): if text1[i-1]==text2[j-1]: dp[i][j] = dp[i-1][j-1] + text1[i-1] else: dp[i][j] = max(dp[i][j-1], dp[i-1][j], key=lambda x: len(x)) return dp[-1][-1] if str1==str2: return str1 i = 0 j = 0 ans = '' for c in lcs(str1, str2): while str1[i]!=c: ans += str1[i] i += 1 while str2[j]!=c: ans += str2[j] j += 1 ans += c i += 1 j += 1 return ans + str1[i:] + str2[j:]
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str: n1, n2 = len(str1), len(str2) dp = [[0 for j in range(n2+1)] for i in range(n1+1)] for i in range(1, n1+1): for j in range(1, n2+1): if str1[i-1] == str2[j-1]: dp[i][j] = dp[i-1][j-1] + 1 else: dp[i][j] = max(dp[i-1][j], dp[i][j-1]) i, j, ans = n1-1, n2-1, '' while i >= 0 and j >= 0: if dp[i+1][j+1] == dp[i][j+1]: ans = str1[i] + ans i -= 1 elif dp[i+1][j+1] == dp[i+1][j]: ans = str2[j] + ans j -= 1 else: ans = str1[i] + ans i -= 1 j -= 1 if i < 0: ans = str2[:j+1] + ans else: ans = str1[:i+1] + ans return ans
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str: step = defaultdict(lambda : None) @lru_cache(None) def cal(i, j): if i >= len(str1): return len(str2)-j if j >= len(str2): return len(str1)-i if str1[i] == str2[j]: step[(i, j)] = 0 return 1 + cal(i+1, j+1) else: a = cal(i+1, j) b = cal(i, j+1) if a < b: step[(i, j)] = 1 return 1 + a else: step[(i, j)] = 2 return 1 + b cal(0, 0) ans = [] def cons(i, j): nonlocal ans if i>=len(str1): ans.append(str2[j:]) return if j>=len(str2): ans.append(str1[i:]) return s = step[(i, j)] if s == 0: ans.append(str1[i]) i+=1 j+=1 elif s == 1: ans.append(str1[i]) i+=1 else: ans.append(str2[j]) j+=1 cons(i, j) cons(0, 0) return ''.join(ans)
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, s1: str, s2: str) -> str: m = {} def fs(a, b): if (a, b) in list(m.keys()): return m[(a, b)] if a == len(s1) or b == len(s2): m[(a, b)] = '' return '' if s1[a] == s2[b]: r = s1[a] + fs(a + 1, b + 1) return r r1 = fs(a + 1, b) r2 = fs(a, b + 1) if len(r1) > len(r2): m[(a, b)] = r1 else: m[(a, b)] = r2 return m[(a, b)] r = fs(0, 0) res = '' i, j = 0, 0 for a in r: while i < len(s1) and s1[i] != a: res += s1[i] i += 1 i += 1 while j < len(s2) and s2[j] != a: res += s2[j] j += 1 j += 1 res += a res += s1[i:] + s2[j:] return res
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str: m = len(str1) n = len(str2) dp = [[0] * (n + 1) for _ in range(m + 1)] for i in range(m): dp[i + 1][0] = dp[i][0] + 1 for i in range(n): dp[0][i + 1] = dp[0][i] + 1 for i in range(m): for j in range(n): if str1[i] == str2[j]: dp[i + 1][j + 1] = dp[i][j] + 1 else: dp[i + 1][j + 1] = min(dp[i][j + 1], dp[i + 1][j]) + 1 ans = [] i = m j = n while i > 0 or j > 0: if i > 0 and j > 0 and dp[i][j] == dp[i - 1][j - 1] + 1 and str1[i - 1] == str2[j - 1]: i -= 1 j -= 1 ans.append(str1[i]) elif i > 0 and dp[i][j] == dp[i - 1][j] + 1: i -= 1 ans.append(str1[i]) elif j > 0 and dp[i][j] == dp[i][j - 1] + 1: j -= 1 ans.append(str2[j]) return ''.join(ans[::-1])
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str: m = len(str1) n = len(str2) dp = [[0 for j in range(n + 1)] for i in range(m + 1)] for i in range(1, m + 1): dp[i][0] = i for j in range(1, n + 1): dp[0][j] = j for i in range(1, m + 1): for j in range(1, n + 1): if str1[i - 1] == str2[j - 1]: dp[i][j] = dp[i - 1][j - 1] + 1 else: dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + 1 ans = '' i, j = m, n while i > 0 and j > 0: if str1[i - 1] == str2[j - 1]: ans += str1[i - 1] i -= 1 j -= 1 elif dp[i][j] == dp[i - 1][j] + 1: ans += str1[i - 1] i -= 1 elif dp[i][j] == dp[i][j - 1] + 1: ans += str2[j - 1] j -= 1 while i > 0: ans += str1[i - 1] i -= 1 while j > 0: ans += str2[j - 1] j -= 1 return ans[::-1]
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str: if not str1: return str2 if not str2: return str1 l1, l2 = len(str1), len(str2) # Step 1. find a longest common subsequence. dp = [''] * (l1+1) for j in range(l2): new_dp = dp[:] for i in range(l1): if str1[i] == str2[j]: new_dp[i+1] = dp[i] + str1[i] elif len(dp[i+1]) < len(new_dp[i]): new_dp[i+1] = new_dp[i] dp = new_dp # Step 2. make a supersequences from the longest common subsequence. ans, i, j = '', 0, 0 for c in dp[-1]: while str1[i] != c: ans += str1[i]; i += 1 while str2[j] != c: ans += str2[j]; j += 1 ans += c; i += 1; j += 1 return ans + str1[i:] + str2[j:]
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, a: str, b: str) -> str: n=len(a) m=len(b) dp=[['-1']*(n+1) for i in range(m+1)] for i in range(m+1): for j in range(n+1): if(i==0 or j==0): dp[i][j]='' else: if(a[j-1]==b[i-1]): dp[i][j]=a[j-1]+dp[i-1][j-1] else: s1=dp[i-1][j] s2=dp[i][j-1] if(len(s1)>len(s2)): dp[i][j]=s1 else: dp[i][j]=s2 s=dp[-1][-1][::-1] result='' I=0 J=0 for i in s: while(a[I]!=i): result=result+a[I] I=I+1 while(b[J]!=i): result=result+b[J] J=J+1 result=result+i I=I+1 J=J+1 return(result+a[I:]+b[J:])
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str: m, n = len(str1), len(str2) dp = [[''] * (n+1) for _ in range(m+1)] for i in range(1,m+1): for j in range(1,n+1): if str1[i-1] == str2[j-1]: dp[i][j] = dp[i-1][j-1] + str1[i-1] else: dp[i][j] = dp[i-1][j] if len(dp[i-1][j]) > len(dp[i][j-1]) else dp[i][j-1] lcs = dp[-1][-1] res, i, j = '', 0, 0 print(lcs) for c in lcs: while str1[i] != c: res += str1[i] i+=1 while str2[j] != c: res += str2[j] j+=1 res += c i, j = i+1, j+1 return res + str1[i:] + str2[j:]