description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: def f(nums1, nums2, i, j, D): if i == 0 or j == 0: return 0 elif str([i, j]) in D: return D[str([i, j])] res = max( nums1[i - 1] * nums2[j - 1] + f(nums1, nums2, i - 1, j - 1, D), f(nums1, nums2, i - 1, j, D), f(nums1, nums2, i, j - 1, D), ) D[str([i, j])] = res return res D = {} res = f(nums1, nums2, len(nums1), len(nums2), D) if res == 0 and (max(nums1) < 0 and min(nums2) > 0): return max(nums1) * min(nums2) elif res == 0 and (max(nums2) < 0 and min(nums1) > 0): return max(nums2) * min(nums1) return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR LIST VAR VAR VAR RETURN VAR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR LIST VAR VAR VAR RETURN VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1, nums2) -> int: n, m = len(nums1), len(nums2) dp1 = [([-float("inf")] * m) for _ in range(n)] dp2 = [([-float("inf")] * m) for _ in range(n)] for i in range(n): for j in range(m): dp1[i][j] = nums1[i] * nums2[j] if i != 0 and j != 0: dp1[i][j] = max(dp1[i][j], dp2[i - 1][j - 1] + nums1[i] * nums2[j]) dp2[i][j] = dp1[i][j] if i != 0: dp2[i][j] = max(dp2[i][j], dp2[i - 1][j]) if j != 0: dp2[i][j] = max(dp2[i][j], dp2[i][j - 1]) ans = -float("inf") for i in range(n): for j in range(m): ans = max(ans, dp2[i][j]) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: dp = collections.defaultdict(lambda: -math.inf) for i, j in product(range(len(nums1)), range(len(nums2))): x, y = nums1[i] * nums2[j], dp[i - 1, j - 1] m = max(x, y) if x < 0 and y < 0 else max(x, 0) + max(y, 0) dp[i, j] = max(dp[i - 1, j], dp[i, j - 1], m) return dp[len(nums1) - 1, len(nums2) - 1]
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1, nums2) -> int: n, m = len(nums1), len(nums2) dp = [[(0) for _ in range(m)] for _ in range(n)] dp[0][0] = nums1[0] * nums2[0] for j in range(1, m): dp[0][j] = max(dp[0][j - 1], nums1[0] * nums2[j]) for i in range(1, n): dp[i][0] = max(dp[i - 1][0], nums1[i] * nums2[0]) for i in range(1, n): for j in range(1, m): dp[i][j] = max( nums1[i] * nums2[j], dp[i - 1][j - 1] + nums1[i] * nums2[j], dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1], ) return dp[n - 1][m - 1]
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: a = [[(-1) for _ in range(len(nums2))] for _ in range(len(nums1))] def dp(x, y): if len(nums1) == x or len(nums2) == y: return -float("inf") if a[x][y] != -1: return a[x][y] else: a[x][y] = max( dp(x + 1, y), dp(x, y + 1), dp(x + 1, y + 1) + nums1[x] * nums2[y], nums1[x] * nums2[y], ) return a[x][y] return dp(0, 0)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR STRING IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR NUMBER NUMBER VAR
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: n1, n2 = len(nums1), len(nums2) mem = [([float("-inf")] * (n2 + 1)) for _ in range(n1 + 1)] mem[0][0] = 0 for i in range(1, n1 + 1): for j in range(1, n2 + 1): mem[i][j] = max( mem[i][j], max( nums1[i - 1] * nums2[j - 1] + max(0, mem[i - 1][j - 1]), mem[i][j - 1], mem[i - 1][j], ), ) return mem[n1][n2] @lru_cache def dp(i1, j1, i2, j2): if i1 > j1 or i2 > j2: return 0 if i1 == j1 and i2 == j2: return nums1[i1] * nums2[i2] if mem[i1][j1][i2][j2] != float("-inf"): return mem[i1][j1][i2][j2] ans = float("-inf") for k1 in range(i1, j1 + 1): for k2 in range(i2, j2 + 1): ans = max( ans, max(0, nums1[k1] * nums2[k2]) + dp(i1, k1 - 1, i2, k2 - 1) + dp(k1 + 1, j1, k2 + 1, j2), ) mem[i1][j1][i2][j2] = ans return ans return dp(0, n1 - 1, 0, n2 - 1)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR VAR FUNC_CALL VAR STRING RETURN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR RETURN VAR VAR RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1, nums2): if all(x >= 0 for x in nums1) and all(y <= 0 for y in nums2): return min(nums1) * max(nums2) if all(x <= 0 for x in nums1) and all(y >= 0 for y in nums2): return max(nums1) * min(nums2) a, b = len(nums1), len(nums2) dp = [[(0) for _ in range(b)] for _ in range(a)] for i in range(a): for j in range(b): dp[i][j] = nums1[i] * nums2[j] if i: dp[i][j] = max(dp[i][j], dp[i - 1][j]) if j: dp[i][j] = max(dp[i][j], dp[i][j - 1]) if i and j: dp[i][j] = max(dp[i][j], nums1[i] * nums2[j] + dp[i - 1][j - 1]) return dp[a - 1][b - 1]
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
MAX_VAL = -(10**9) - 7 class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: num_rows = len(nums1) + 1 num_cols = len(nums2) + 1 dp = [] for i in range(num_rows): dp.append([MAX_VAL] * num_cols) for i in range(1, num_rows): for j in range(1, num_cols): dp[i][j] = max( dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1] + nums1[i - 1] * nums2[j - 1], nums1[i - 1] * nums2[j - 1], ) return dp[-1][-1]
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR NUMBER NUMBER VAR
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: if all(x >= 0 for x in nums1) and all(y <= 0 for y in nums2): return min(nums1) * max(nums2) if all(x <= 0 for x in nums1) and all(y >= 0 for y in nums2): return max(nums1) * min(nums2) accu_max = [0] * (len(nums2) + 1) for i in range(len(nums1)): last_max = [(accu_max[j] + nums1[i] * nums2[j]) for j in range(len(nums2))] this_accu_max = [0] for j in range(len(nums2)): this_accu_max.append( max(this_accu_max[-1], last_max[j], accu_max[j + 1]) ) accu_max = this_accu_max return accu_max[-1]
CLASS_DEF FUNC_DEF VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR NUMBER VAR
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: if not nums1: return 0 cache = [[(0) for _ in nums2] for _ in nums1] cache[-1][-1] = nums1[-1] * nums2[-1] for i in range(len(nums1) - 2, -1, -1): cache[i][-1] = max(nums1[i] * nums2[-1], cache[i + 1][-1]) for i in range(len(nums2) - 2, -1, -1): cache[-1][i] = max(nums1[-1] * nums2[i], cache[-1][i + 1]) for i in range(len(nums1) - 2, -1, -1): for j in range(len(nums2) - 2, -1, -1): cache[i][j] = max( nums1[i] * nums2[j] + max(cache[i + 1][j + 1], 0), cache[i + 1][j], cache[i][j + 1], ) return cache[0][0]
CLASS_DEF FUNC_DEF VAR VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER NUMBER VAR
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: n1 = len(nums1) n2 = len(nums2) dp = [[(0) for _ in range(n2)] for _ in range(n1)] for i in range(n1): for j in range(n2): curr = nums1[i] * nums2[j] if i == 0 and j == 0: dp[i][j] = curr elif i == 0: dp[i][j] = max(curr, dp[i][j - 1]) elif j == 0: dp[i][j] = max(curr, dp[i - 1][j]) else: dp[i][j] = max( curr, curr + dp[i - 1][j - 1], dp[i - 1][j], dp[i][j - 1] ) return dp[n1 - 1][n2 - 1]
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: n1 = len(nums1) n2 = len(nums2) dp = [([0] * (n2 + 1)) for j in range(n1 + 1)] for i in range(n1): for j in range(n2): dp[i + 1][j + 1] = max( dp[i][j] + max(0, nums1[i] * nums2[j]), dp[i][j + 1], dp[i + 1][j] ) if dp[-1][-1] != 0: return dp[-1][-1] nums1.sort() nums2.sort() a = nums1[0] if nums1[0] > 0 else nums1[-1] b = nums2[0] if nums2[0] > 0 else nums2[-1] return a * b
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER NUMBER NUMBER RETURN VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER RETURN BIN_OP VAR VAR VAR
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: dp = [] for i in range(len(nums1)): dp.append([]) for j in range(len(nums2)): dp[i].append(0) for i in range(len(nums1)): for j in range(len(nums2)): dp[i][j] = nums1[i] * nums2[j] if i and j: dp[i][j] += max(dp[i - 1][j - 1], 0) if i: dp[i][j] = max(dp[i][j], dp[i - 1][j]) if j: dp[i][j] = max(dp[i][j], dp[i][j - 1]) return dp[-1][-1]
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER NUMBER VAR
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1, nums2): m = len(nums1) n = len(nums2) mem = [([None] * n) for i in range(m)] for i in range(m - 1, -1, -1): for j in range(n - 1, -1, -1): mem[i][j] = nums1[i] * nums2[j] for i in range(m - 1, -1, -1): for j in range(n - 1, -1, -1): max_ = mem[i + 1][j + 1] if i + 1 < m and j + 1 < n else -1001 mem[i][j] = max(mem[i][j] + max_, max(mem[i][j], max_)) mem[i][j] = max(mem[i][j], mem[i][j + 1] if j < n - 1 else -1001) mem[i][j] = max(mem[i][j], mem[i + 1][j] if i < m - 1 else -1001) return mem[0][0]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NONE VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR NUMBER NUMBER
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: max_dot_product = [(nums1[0] * num) for num in nums2] max_dot_product = list(itertools.accumulate(max_dot_product, max)) for num1 in nums1[1:]: max_dot_product_next = [] for i, num2 in enumerate(nums2): curr_max = max_dot_product[i] if i > 0: curr_max = max( curr_max, max( num1 * num2 + max_dot_product[i - 1], max_dot_product_next[i - 1], ), ) curr_max = max(curr_max, num1 * num2) max_dot_product_next.append(curr_max) max_dot_product = max_dot_product_next return max(max_dot_product)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: dic = {} dic[0, 0] = nums1[0] * nums2[0] def search(i, j): a = nums1[i] * nums2[j] if (i, j) in dic: return dic[i, j] if i == 0: dic[0, j] = max(search(0, j - 1), a) return dic[i, j] elif j == 0: dic[i, 0] = max(search(i - 1, 0), a) return dic[i, 0] else: dic[i, j] = max( search(i - 1, j), search(i, j - 1), a + search(i - 1, j - 1), a ) return dic[i, j] return search(len(nums1) - 1, len(nums2) - 1)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR RETURN VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR RETURN VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: dp = [([0] * len(nums2)) for _ in range(len(nums1))] for i in range(len(nums1)): for j in range(len(nums2)): if i == 0 and j == 0: dp[i][j] = nums1[0] * nums2[0] continue prod = nums1[i] * nums2[j] if i == 0: dp[i][j] = max(dp[i][j - 1], prod) continue if j == 0: dp[i][j] = max(dp[i - 1][j], prod) continue dp[i][j] = max( dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1] + prod, prod ) return dp[-1][-1]
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR RETURN VAR NUMBER NUMBER VAR
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
def max_dot_product(A, B): mem = {} for i in range(len(A)): for j in range(len(B)): if i == j == 0: ans = A[i] * B[j] elif i == 0: ans = max(A[i] * B[j], mem[i, j - 1]) elif j == 0: ans = max(A[i] * B[j], mem[i - 1, j]) else: ans = max( A[i] * B[j], A[i] * B[j] + mem[i - 1, j - 1], mem[i, j - 1], mem[i - 1, j], ) mem[i, j] = ans return mem[len(A) - 1, len(B) - 1] class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: return max_dot_product(nums1, nums2)
FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR RETURN VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER CLASS_DEF FUNC_DEF VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, a: List[int], b: List[int]) -> int: nums1 = [0] + a nums2 = [0] + b m, n = len(nums1), len(nums2) dp = [([float("-inf")] * m) for _ in range(n)] for i in range(1, n): for j in range(1, m): if nums1[j] * nums2[i] > 0: dp[i][j] = max( dp[i - 1][j - 1] + nums1[j] * nums2[i], dp[i][j - 1], dp[i - 1][j], nums1[j] * nums2[i], ) else: dp[i][j] = max(dp[i][j - 1], dp[i - 1][j], nums1[j] * nums2[i]) return dp[-1][-1]
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR RETURN VAR NUMBER NUMBER VAR
Given two arrays nums1 and nums2. Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length. A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).   Example 1: Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] Output: 18 Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. Their dot product is (2*3 + (-2)*(-6)) = 18. Example 2: Input: nums1 = [3,-2], nums2 = [2,-6,7] Output: 21 Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. Their dot product is (3*7) = 21. Example 3: Input: nums1 = [-1,-1], nums2 = [1,1] Output: -1 Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. Their dot product is -1.   Constraints: 1 <= nums1.length, nums2.length <= 500 -1000 <= nums1[i], nums2[i] <= 1000
class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: n1: int = len(nums1) n2: int = len(nums2) dp: List[List[int]] = [ [float("-inf") for _ in range(n2 + 1)] for _ in range(n1 + 1) ] for i in range(1, n1 + 1): for j in range(1, n2 + 1): dp[i][j] = max( dp[i - 1][j], dp[i][j - 1], max(0, dp[i - 1][j - 1]) + nums1[i - 1] * nums2[j - 1], ) return dp[n1][n2]
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR VAR
Given two strings X and Y, the task is to check if it is possible to convert X to Y by performing the following operations. Make some lowercase letters uppercase. Delete all the lowercase letters. NOTE: You can perform one,two or none operations to convert the string X to Y as needed. Example 1: Input: X = "daBcd", Y = "ABC" Output: 1 Explanation: Convert 'a' and 'c', delete both the d's Example 2: Input: X = "ABcd", Y = "BCD" Output: 0 Explanation: Can not delete A Your Task: You don't need to read input or print anything. Your task is to complete the function stringConversion() which takes the strings as input and returns 1 if it is possible to convert, otherwise 0. Expected Time Complexity: O(|X|*|Y|) Expected Auxiliary Space: O(|X|*|Y|) Constraints: 1 ≤ |X|, |Y| ≤ 10^{3}
class Solution: def stringConversion(self, X, Y): n = len(X) m = len(Y) dp = [[(0) for i in range(m + 1)] for j in range(n + 1)] dp[0][0] = 1 for i in range(1, n + 1): dp[i][0] = dp[i - 1][0] if X[i - 1].islower() else 0 for i in range(1, n + 1): for j in range(1, m + 1): if X[i - 1].upper() == Y[j - 1]: dp[i][j] = dp[i - 1][j - 1] if X[i - 1].islower(): dp[i][j] = dp[i][j] or dp[i - 1][j] return dp[n][m]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR
Given two strings X and Y, the task is to check if it is possible to convert X to Y by performing the following operations. Make some lowercase letters uppercase. Delete all the lowercase letters. NOTE: You can perform one,two or none operations to convert the string X to Y as needed. Example 1: Input: X = "daBcd", Y = "ABC" Output: 1 Explanation: Convert 'a' and 'c', delete both the d's Example 2: Input: X = "ABcd", Y = "BCD" Output: 0 Explanation: Can not delete A Your Task: You don't need to read input or print anything. Your task is to complete the function stringConversion() which takes the strings as input and returns 1 if it is possible to convert, otherwise 0. Expected Time Complexity: O(|X|*|Y|) Expected Auxiliary Space: O(|X|*|Y|) Constraints: 1 ≤ |X|, |Y| ≤ 10^{3}
class Solution: def stringConversion(self, X, Y): s1 = X s2 = Y n = len(s1) m = len(s2) dp = [[(False) for i in range(m + 1)] for i in range(n + 1)] dp[0][0] = True for i in range(len(s1)): for j in range(len(s2) + 1): if dp[i][j]: if j < len(s2) and s1[i].upper() == s2[j]: dp[i + 1][j + 1] = True if s1[i].isupper() == False: dp[i + 1][j] = True if dp[n][m]: return 1 else: return 0
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR IF VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR RETURN NUMBER RETURN NUMBER
As you know, every birthday party has a cake! This time, Babaei is going to prepare the very special birthday party's cake. Simple cake is a cylinder of some radius and height. The volume of the simple cake is equal to the volume of corresponding cylinder. Babaei has n simple cakes and he is going to make a special cake placing some cylinders on each other. However, there are some additional culinary restrictions. The cakes are numbered in such a way that the cake number i can be placed only on the table or on some cake number j where j < i. Moreover, in order to impress friends Babaei will put the cake i on top of the cake j only if the volume of the cake i is strictly greater than the volume of the cake j. Babaei wants to prepare a birthday cake that has a maximum possible total volume. Help him find this value. -----Input----- The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of simple cakes Babaei has. Each of the following n lines contains two integers r_{i} and h_{i} (1 ≤ r_{i}, h_{i} ≤ 10 000), giving the radius and height of the i-th cake. -----Output----- Print the maximum volume of the cake that Babaei can make. Your answer will be considered correct if its absolute or relative error does not exceed 10^{ - 6}. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$. -----Examples----- Input 2 100 30 40 10 Output 942477.796077000 Input 4 1 1 9 7 1 4 10 7 Output 3983.539484752 -----Note----- In first sample, the optimal way is to choose the cake number 1. In second sample, the way to get the maximum volume is to use cakes with indices 1, 2 and 4.
from sys import * def add(i, q): while i < len(d): d[i] = max(d[i], q) i += i & -i def get(i): q = 0 while i > 0: q = max(d[i], q) i -= i & -i return q t = list(map(int, stdin.read().split())) p = [(t[i] * t[i] * t[i + 1]) for i in range(1, len(t), 2)] k = {v: j for j, v in enumerate(sorted(set(p)))} d = [0] * (len(k) + 1) for v in p: j = k[v] add(j + 1, get(j) + v) print(get(len(k)) * 3.14159265)
FUNC_DEF WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def util(self, a, i, j, n, m): global dp if i >= n or i < 0 or j >= m or j < 0: return 0 if dp[i][j] != -1: return dp[i][j] dp[i][j] = a[i][j] + max( self.util(a, i + 1, j, n, m), max(self.util(a, i + 1, j + 1, n, m), self.util(a, i + 1, j - 1, n, m)), ) return dp[i][j] def maximumPath(self, N, Matrix): ans = 0 global dp dp = [[(-1) for i in range(N)] for j in range(N)] for i in range(N): ans = max(ans, self.util(Matrix, 0, i, N, N)) return ans
CLASS_DEF FUNC_DEF IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR RETURN VAR
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, n, a): dp = [[(-1) for _ in range(n)] for _ in range(n)] for j in range(n): dp[0][j] = a[0][j] for i in range(1, n): for j in range(n): up = a[i][j] + dp[i - 1][j] ld = -1000000000.0 if j - 1 >= 0: ld = a[i][j] + dp[i - 1][j - 1] rd = -1000000000.0 if j + 1 < n: rd = a[i][j] + dp[i - 1][j + 1] dp[i][j] = max(up, ld, rd) return max([dp[n - 1][j] for j in range(n)])
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, N, Matrix): dplast = [(0) for i in range(N)] for i in range(N - 1, -1, -1): newdp = [] for j in range(N): best = dplast[j] if j > 0: best = max(best, dplast[j - 1]) if j < N - 1: best = max(best, dplast[j + 1]) newdp.append(best + Matrix[i][j]) dplast = newdp return max(dplast)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, N, Matrix): for i in range(N - 2, -1, -1): for j in range(N): if 0 < j < N - 1: Matrix[i][j] += max( max(Matrix[i + 1][j - 1], Matrix[i + 1][j]), Matrix[i + 1][j + 1], ) elif j == 0: Matrix[i][j] += max(Matrix[i + 1][j + 1], Matrix[i + 1][j]) elif j == N - 1: Matrix[i][j] += max(Matrix[i + 1][j - 1], Matrix[i + 1][j]) return max(Matrix[0])
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR NUMBER
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, N, matrix): max_res = 0 arr = [[(-1) for i in range(len(matrix))] for j in range(len(matrix))] for i in range(len(matrix)): max_res = max(max_res, self.maxPathHelper(matrix, 0, i, arr)) return max_res def maxPathHelper(self, matrix, row, col, arr): if row == len(matrix): return 0 if col < 0 or col >= len(matrix): return 0 if arr[row][col] != -1: return arr[row][col] res1 = matrix[row][col] + self.maxPathHelper(matrix, row + 1, col, arr) res2 = matrix[row][col] + self.maxPathHelper(matrix, row + 1, col - 1, arr) res3 = matrix[row][col] + self.maxPathHelper(matrix, row + 1, col + 1, arr) res = max(res1, res2, res3) arr[row][col] = res return res
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR RETURN VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, N, grid): n = len(grid) m = len(grid[0]) dp = [[(-1) for j in range(m)] for i in range(n)] maxi = float("-inf") for j in range(m): maxi = max(maxi, self.solve(n - 1, j, m, grid, dp)) return maxi def solve(self, i, j, m, grid, dp): if j < 0 or j >= m: return float("-inf") if i == 0: return grid[0][j] if dp[i][j] != -1: return dp[i][j] path = ( max( self.solve(i - 1, j, m, grid, dp), self.solve(i - 1, j - 1, m, grid, dp), self.solve(i - 1, j + 1, m, grid, dp), ) + grid[i][j] ) dp[i][j] = path return dp[i][j]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER VAR VAR RETURN FUNC_CALL VAR STRING IF VAR NUMBER RETURN VAR NUMBER VAR IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, N, Matrix): dp = [([-1] * N) for i in range(N)] def uniq(i, j): if j < 0 or j > N - 1: return 0 elif i == 0: return Matrix[i][j] if dp[i][j] != -1: return dp[i][j] dp[i][j] = Matrix[i][j] + max( uniq(i - 1, j), uniq(i - 1, j - 1), uniq(i - 1, j + 1) ) return dp[i][j] maxi = 0 for col in range(N): maxi = max(maxi, uniq(N - 1, col)) return maxi
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
import sys class Solution: def maximumPath(self, n, Matrix): dp = [[(-1) for i in range(n)] for j in range(n)] for i in range(n): dp[0][i] = Matrix[0][i] for i in range(1, n): for j in range(n): dp[i][j] = dp[i - 1][j] if j - 1 >= 0: dp[i][j] = max(dp[i][j], dp[i - 1][j - 1]) if j + 1 < n: dp[i][j] = max(dp[i][j], dp[i - 1][j + 1]) dp[i][j] += Matrix[i][j] maxi = -sys.maxsize for i in range(n): maxi = max(dp[n - 1][i], maxi) return maxi
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, N, matrix): maxx = 0 dp = [[(0) for _ in range(N)] for _ in range(N)] for row in range(N): for col in range(N): if row == 0: dp[row][col] = matrix[row][col] else: down = matrix[row][col] + dp[row - 1][col] if col < N - 1: downLeft = matrix[row][col] + dp[row - 1][col + 1] else: downLeft = 0 if col > 0: downRight = matrix[row][col] + dp[row - 1][col - 1] else: downRight = 0 dp[row][col] = max(down, downLeft, downRight) for idx in range(N): maxx = max(dp[-1][idx], maxx) return maxx def findMax(matrix, dp, row, col): if col < 0 or col >= len(matrix[0]): return 0 if row == len(matrix) - 1: return matrix[row][col] if (row, col) in dp: return dp[row, col] down = matrix[row][col] + findMax(matrix, dp, row + 1, col) downLeft = matrix[row][col] + findMax(matrix, dp, row + 1, col - 1) downRight = matrix[row][col] + findMax(matrix, dp, row + 1, col + 1) dp[row, col] = max(down, downLeft, downRight) return dp[row, col]
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR VAR
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, N, Matrix): a = [[(0) for hm in range(N + 1)] for hm in range(N + 1)] for hm in range(N - 1, -1, -1): for gm in range(N - 1, -1, -1): a[hm][gm] = Matrix[hm][gm] + max( a[hm + 1][gm], a[hm + 1][gm - 1], a[hm + 1][gm + 1] ) return max(a[0])
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, n, a): dp = [[(0) for _ in range(n)] for _ in range(n)] for i in range(n): for j in range(n): if i == 0: l = r = d = 0 elif j == 0 and j < n - 1: d = dp[i - 1][j] r = dp[i - 1][j + 1] l = 0 elif j == n - 1 and j > 0: d = dp[i - 1][j] l = dp[i - 1][j - 1] r = 0 else: d = dp[i - 1][j] r = dp[i - 1][j + 1] l = dp[i - 1][j - 1] dp[i][j] = a[i][j] + max(l, r, d) return max(dp[-1])
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, N, Matrix): dp = [[(0) for _ in range(N)] for _ in range(N)] for i in range(N): dp[0][i] = Matrix[0][i] for i in range(1, N): for j in range(0, N): if j == 0: dp[i][j] = Matrix[i][j] + max(dp[i - 1][j], dp[i - 1][j + 1]) elif j == N - 1: dp[i][j] = Matrix[i][j] + max(dp[i - 1][j], dp[i - 1][j - 1]) else: dp[i][j] = Matrix[i][j] + max( dp[i - 1][j - 1], dp[i - 1][j], dp[i - 1][j + 1] ) rst = 0 for i in range(N): rst = max(rst, dp[N - 1][i]) return rst
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, N, Matrix): dp = [[(0) for _ in range(N)] for _ in range(N)] dp[0] = Matrix[0] for r in range(1, N): for c in range(N): temp = float("-inf") dir = [-1, 0, 1] for d in dir: if 0 <= c + d < N: temp = max(temp, dp[r - 1][c + d]) dp[r][c] = Matrix[r][c] + temp return max(dp[N - 1])
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR VAR IF NUMBER BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, N, Matrix): if N == 1: return max(Matrix[0]) ans = -1 for i in range(1, N): for j in range(N): if j > 0 and j < N - 1: cur = max( Matrix[i - 1][j - 1], Matrix[i - 1][j + 1], Matrix[i - 1][j] ) Matrix[i][j] += cur ans = max(ans, Matrix[i][j]) elif j > 0: cur = max(Matrix[i - 1][j - 1], Matrix[i - 1][j]) Matrix[i][j] += cur ans = max(Matrix[i][j], ans) elif j < N - 1: cur = max(Matrix[i - 1][j + 1], Matrix[i - 1][j]) Matrix[i][j] += cur ans = max(Matrix[i][j], ans) return ans
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, N, Matrix): for i in range(1, N): for j in range(0, N): if j == 0 or j == N - 1: if j == 0: Matrix[i][j] += max(Matrix[i - 1][j], Matrix[i - 1][j + 1]) else: Matrix[i][j] += max(Matrix[i - 1][j], Matrix[i - 1][j - 1]) else: Matrix[i][j] += max( Matrix[i - 1][j], Matrix[i - 1][j - 1], Matrix[i - 1][j + 1] ) return max(Matrix[N - 1])
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, N, Matrix): dpTable = [[(0) for _ in range(N)] for _ in range(N + 1)] for row in range(1, N + 1): for col in range(N): maxValue = dpTable[row - 1][col] if col > 0: maxValue = max(maxValue, dpTable[row - 1][col - 1]) if col < N - 1: maxValue = max(maxValue, dpTable[row - 1][col + 1]) dpTable[row][col] = maxValue + Matrix[row - 1][col] return max(dpTable[-1])
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR NUMBER
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, N, Matrix): arr = [] for i in range(0, N): w = [] for j in range(0, N): w.append(-1) arr.append(w) def wtf(Matrix, arr, col, v): if v == N - 1: return Matrix[v][col] if arr[v][col] != -1: return arr[v][col] x = 0 z = 0 if col > 0: x = Matrix[v][col] + wtf(Matrix, arr, col - 1, v + 1) y = wtf(Matrix, arr, col, v + 1) + Matrix[v][col] if col < N - 1: z = wtf(Matrix, arr, col + 1, v + 1) + Matrix[v][col] arr[v][col] = max(x, y, z) return arr[v][col] ans = [] for i in range(0, N): s = wtf(Matrix, arr, i, 0) ans.append(s) return max(ans)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: dp = [] def gen(self, n, matrix, x, y): if x > n or y > n or y < 0: return 0 if x == n: return matrix[x][y] p = 0 q = 0 r = 0 if self.dp[x][y] != -1: return self.dp[x][y] if x + 1 <= n: p = self.gen(n, matrix, x + 1, y) + matrix[x][y] if x + 1 <= n and y - 1 >= 0: q = self.gen(n, matrix, x + 1, y - 1) + matrix[x][y] if x + 1 <= n and y + 1 <= n: r = self.gen(n, matrix, x + 1, y + 1) + matrix[x][y] self.dp[x][y] = max(p, q, r) return max(p, q, r) def maximumPath(self, N, Matrix): dp = [] for i in range(N): arr = [] for j in range(N): arr.append(-1) dp.append(arr) for i in range(N): dp[N - 1][i] = Matrix[N - 1][i] for i in range(N - 2, -1, -1): for j in range(N): if j == 0: dp[i][j] = max(dp[i + 1][j], dp[i + 1][j + 1]) + Matrix[i][j] elif j == N - 1: dp[i][j] = max(dp[i + 1][j], dp[i + 1][j - 1]) + Matrix[i][j] else: dp[i][j] = ( max(dp[i + 1][j - 1], dp[i + 1][j], dp[i + 1][j + 1]) + Matrix[i][j] ) return max(dp[0])
CLASS_DEF ASSIGN VAR LIST FUNC_DEF IF VAR VAR VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, n, arr): if n == 1: return arr[0][0] for r in range(n - 2, -1, -1): for c in range(n - 1, -1, -1): temp = arr[r][c] arr[r][c] += arr[r + 1][c] if c + 1 < n: arr[r][c] = max(arr[r][c], temp + arr[r + 1][c + 1]) if c - 1 >= 0: arr[r][c] = max(arr[r][c], temp + arr[r + 1][c - 1]) return max(arr[0])
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def solve(self, r, c, matrix, box): if c < 0 or c >= len(matrix): return -1 if r == len(matrix) - 1: return matrix[r][c] if box[r][c] != -1: return box[r][c] box[r][c] = matrix[r][c] + max( self.solve(r + 1, c, matrix, box), self.solve(r + 1, c + 1, matrix, box), self.solve(r + 1, c - 1, matrix, box), ) return box[r][c] def maximumPath(self, N, Matrix): box = [[(-1) for _ in range(N)] for _ in range(N)] ans = 0 for c in range(N): ans = max(ans, self.solve(0, c, Matrix, box)) return ans
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER VAR VAR VAR RETURN VAR
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, n, mat): n = len(mat) dp = [([-1] * n) for i in range(n)] def findmaxpath(i, j, n): if i < 0 or j < 0 or i >= n or j >= n: return 0 if dp[i][j] != -1: return dp[i][j] val = 0 val = findmaxpath(i + 1, j, n) val = max(val, findmaxpath(i + 1, j - 1, n)) val = max(val, findmaxpath(i + 1, j + 1, n)) dp[i][j] = val + mat[i][j] return dp[i][j] maxval = 0 for i in range(n): maxval = max(maxval, findmaxpath(0, i, n)) return maxval
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER VAR VAR RETURN VAR
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
import sys class Solution: def maximumPath(self, n, mat): import sys sys.setrecursionlimit(10**9) dp = [[(-sys.maxsize) for j in range(n)] for i in range(n)] for i in range(n): for j in range(n): if i == 0: dp[i][j] = mat[i][j] continue up = mat[i][j] + dp[i - 1][j] uld = urd = -sys.maxsize if j - 1 >= 0: uld = mat[i][j] + dp[i - 1][j - 1] if j + 1 < n: urd = mat[i][j] + dp[i - 1][j + 1] dp[i][j] = max(dp[i][j], max(up, max(urd, uld))) return max(dp[n - 1]) def solve(self, i, j, n, mat, dp): import sys if i == n - 1: dp[i][j] = mat[i][j] return mat[i][j] if dp[i][j] != -sys.maxsize: return dp[i][j] ld = rd = -sys.maxsize down = mat[i][j] + self.solve(i + 1, j, n, mat, dp) if j - 1 >= 0: ld = mat[i][j] + self.solve(i + 1, j - 1, n, mat, dp) if j + 1 < n: rd = mat[i][j] + self.solve(i + 1, j + 1, n, mat, dp) max_ = max(down, max(ld, rd)) dp[i][j] = max(dp[i][j], max_) return dp[i][j]
IMPORT CLASS_DEF FUNC_DEF IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF IMPORT IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR IF VAR VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR VAR
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, N, mat): def calc(r, c, dp): if dp[r][c] != -1: return dp[r][c] if r == 0: return mat[r][c] l1, l2, l3 = float("-inf"), float("-inf"), float("-inf") if r - 1 >= 0 and c - 1 >= 0: l1 = mat[r][c] + calc(r - 1, c - 1, dp) if r - 1 >= 0: l2 = mat[r][c] + calc(r - 1, c, dp) if c + 1 < len(mat[0]): l3 = mat[r][c] + calc(r - 1, c + 1, dp) dp[r][c] = max(l1, l2, l3) return dp[r][c] dp = [[(-1) for i in range(N)] for j in range(N)] ma = float("-inf") for i in range(N): ma = max(ma, calc(N - 1, i, dp)) return ma
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, N, Matrix): dp = [[(-1) for i in range(N)] for j in range(N)] def maxPath(dp, r, c): if r < 0 or c < 0 or r >= N or c >= N: return -99999 if r == N - 1: return Matrix[r][c] if dp[r][c] != -1: return dp[r][c] else: option1 = maxPath(dp, r + 1, c) option2 = maxPath(dp, r + 1, c - 1) option3 = maxPath(dp, r + 1, c + 1) dp[r][c] = Matrix[r][c] + max([option1, option2, option3]) return dp[r][c] maxi = -9999 for i in range(0, N): maxi = max(maxi, maxPath(dp, 0, i)) return maxi
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR RETURN NUMBER IF VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR LIST VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR RETURN VAR
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, N, Matrix): dp = [[(-1) for i in range(N)] for j in range(N)] maxi = 0 for j in range(N): dp[0][j] = Matrix[0][j] for i in range(1, N): for j in range(N): ld = Matrix[i][j] if j - 1 < 0: ld = ld + float("-inf") else: ld = ld + dp[i - 1][j - 1] up = Matrix[i][j] + dp[i - 1][j] rd = Matrix[i][j] if j + 1 >= N: rd = rd + float("-inf") else: rd = rd + dp[i - 1][j + 1] dp[i][j] = max(ld, up, rd) for j in range(N): maxi = max(maxi, dp[N - 1][j]) return maxi
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, N, arr): dp = [([0] * len(arr[0])) for x in range(len(arr))] for i in range(len(arr[0])): dp[-1][i] = arr[-1][i] for i in range(len(arr) - 2, -1, -1): for j in range(len(arr[0])): x, y, z = 0, 0, 0 x = arr[i][j] + dp[i + 1][j] if j + 1 < len(arr[0]): y = arr[i][j] + dp[i + 1][j + 1] if j - 1 >= 0: z = arr[i][j] + dp[i + 1][j - 1] dp[i][j] = max(x, y, z) return max(dp[0])
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, N, matrix): n, m = len(matrix), len(matrix[0]) dp = [([-1] * m) for _ in range(n)] def f(i, j): if j < 0 or j >= m: return -1000000000.0 if i == 0: return matrix[0][j] if dp[i][j] != -1: return dp[i][j] s = matrix[i][j] + f(i - 1, j) l = matrix[i][j] + f(i - 1, j - 1) r = matrix[i][j] + f(i - 1, j + 1) dp[i][j] = max(max(s, l), r) return dp[i][j] ans = -1000000000.0 for j in range(m): ans = max(ans, f(n - 1, j)) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER VAR VAR RETURN NUMBER IF VAR NUMBER RETURN VAR NUMBER VAR IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, n, matrix): if n == 1: return matrix[0][0] dp = [([0] * n) for _ in range(n)] for i in range(n): dp[0][i] = matrix[0][i] for i in range(1, n): for j in range(n): l = [[-1, 0], [-1, -1], [-1, 1]] for a, b in l: nr, nc = i + a, b + j if nr < n and nr >= 0 and nc < n and nc >= 0: dp[i][j] = max(dp[i][j], dp[nr][nc] + matrix[i][j]) return max(dp[n - 1])
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, N, Matrix): def Solve(dp, i, j): if i < 0 or j < 0 or j >= N: return 0 if dp[i][j] != -1: return dp[i][j] if i == 0: return Matrix[0][j] dp[i][j] = ( max( Solve(dp, i - 1, j - 1), Solve(dp, i - 1, j), Solve(dp, i - 1, j + 1), ) + Matrix[i][j] ) return dp[i][j] ans = 0 dp = [] for i in range(N): dp.append([-1] * N) for i in range(N): for j in range(N): if i == 0: dp[i][j] = Matrix[i][j] else: left = dp[i - 1][max(j - 1, 0)] right = dp[i - 1][min(N - 1, j + 1)] same = dp[i - 1][j] dp[i][j] = max(left, right, same) + Matrix[i][j] if i == N - 1: ans = max(ans, dp[i][j]) return ans
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR NUMBER RETURN VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, N, Matrix): dp = [[(0) for i in range(N)] for i in range(N)] for i in range(N): dp[0][i] = Matrix[0][i] for i in range(1, N): for j in range(N): if j == 0 and j + 1 < N: dp[i][j] = Matrix[i][j] + max(dp[i - 1][j], dp[i - 1][j + 1]) elif j - 1 >= 0 and j == N - 1: dp[i][j] = Matrix[i][j] + max(dp[i - 1][j], dp[i - 1][j - 1]) elif j > 0 and j + 1 < N: dp[i][j] = Matrix[i][j] + max( dp[i - 1][j], max(dp[i - 1][j - 1], dp[i - 1][j + 1]) ) else: dp[i][j] = Matrix[i][j] + dp[i - 1][j] maxi = 0 for i in range(N): maxi = max(maxi, dp[N - 1][i]) return maxi
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, N, arr): for i in range(1, len(arr)): for j in range(len(arr[0])): if j == 0: arr[i][j] += max(arr[i - 1][j + 1], arr[i - 1][j]) elif j == len(arr) - 1: arr[i][j] += max(arr[i - 1][j], arr[i - 1][j - 1]) else: arr[i][j] += max( arr[i - 1][j - 1], arr[i - 1][j], arr[i - 1][j + 1] ) return max(arr[-1])
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
import sys class Solution: def maximumPath(self, N, Matrix): def path(i, j, N, Matrix, dp): if j < 0 or j >= N: return -sys.maxsize if i == 0: return Matrix[i][j] if dp[i][j] != -1: return dp[i][j] s = Matrix[i][j] + path(i - 1, j, N, Matrix, dp) l = Matrix[i][j] + path(i - 1, j - 1, N, Matrix, dp) r = Matrix[i][j] + path(i - 1, j + 1, N, Matrix, dp) dp[i][j] = max(s, l, r) return dp[i][j] dp = [[(-1) for i in range(N)] for j in range(N)] s = -sys.maxsize for i in range(N): m = path(N - 1, i, N, Matrix, dp) s = max(s, m) return s
IMPORT CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER VAR VAR RETURN VAR IF VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, N, Matrix): def Solve(dp, i, j): if i < 0 or j < 0 or j >= N: return 0 if dp[i][j] != -1: return dp[i][j] if i == 0: return Matrix[0][j] dp[i][j] = ( max( Solve(dp, i - 1, j - 1), Solve(dp, i - 1, j), Solve(dp, i - 1, j + 1), ) + Matrix[i][j] ) return dp[i][j] ans = 0 dp = [] for i in range(N): dp.append([-1] * N) for i in range(N): ans = max(ans, Solve(dp, N - 1, i)) return ans
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR NUMBER RETURN VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
def solve(arr, i, j): if j < 0 or j > len(arr) - 1: return 0 elif i == len(arr) - 1: return arr[i][j] elif f[i][j] != -1: return f[i][j] path1 = solve(arr, i + 1, j - 1) path2 = solve(arr, i + 1, j) path3 = solve(arr, i + 1, j + 1) f[i][j] = arr[i][j] + max(path1, path2, path3) return f[i][j] class Solution: def maximumPath(self, N, Matrix): global f f = [[(-1) for i in range(N)] for j in range(N)] ans = 0 for j in range(N): ans = max(ans, solve(Matrix, 0, j)) return ans
FUNC_DEF IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR RETURN VAR
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, N, Matrix): row = len(Matrix) col = len(Matrix[0]) dp = [[(-1) for i in range(col)] for j in range(row)] result = -1 for i in range(row): for j in range(col): if i == 0: dp[i][j] = Matrix[i][j] elif j - 1 < 0: dp[i][j] = Matrix[i][j] + max(dp[i - 1][j], dp[i - 1][j + 1]) elif j + 1 >= col: dp[i][j] = Matrix[i][j] + max(dp[i - 1][j], dp[i - 1][j - 1]) else: dp[i][j] = Matrix[i][j] + max( dp[i - 1][j], dp[i - 1][j + 1], dp[i - 1][j - 1] ) for i in range(col): result = max(result, dp[row - 1][i]) return result
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, N, Matrix): aux = [[Matrix[i][j] for j in range(N)] for i in range(N)] def isValid(i, j): return 0 <= i and i < N and 0 <= j and j < N for i in range(N): for j in range(N): if isValid(i + 1, j): aux[i + 1][j] = max(aux[i + 1][j], Matrix[i + 1][j] + aux[i][j]) if isValid(i + 1, j - 1): aux[i + 1][j - 1] = max( aux[i + 1][j - 1], Matrix[i + 1][j - 1] + aux[i][j] ) if isValid(i + 1, j + 1): aux[i + 1][j + 1] = max( aux[i + 1][j + 1], Matrix[i + 1][j + 1] + aux[i][j] ) maxpath = 0 for i in range(N): maxpath = max(maxpath, aux[N - 1][i]) return maxpath
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN NUMBER VAR VAR VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, N, Matrix): dp = [[(-1) for i in range(N)] for j in range(N)] def helper(i, j): if i >= N or j < 0 or j >= N: return float("-inf") if i == N - 1: return Matrix[i][j] if dp[i][j] != -1: return dp[i][j] dL = Matrix[i][j] + helper(i + 1, j - 1) d = Matrix[i][j] + helper(i + 1, j) dR = Matrix[i][j] + helper(i + 1, j + 1) dp[i][j] = max(dL, max(d, dR)) return dp[i][j] ans = float("-inf") for j in range(N): ans = max(ans, helper(0, j)) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR NUMBER VAR VAR RETURN FUNC_CALL VAR STRING IF VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER VAR RETURN VAR
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, N, Matrix): def helper(i, j): if i == N: return 0 if i >= N or j < 0 or j >= N: return float("-inf") if dp[i + 1][j] == -1: ans1 = helper(i + 1, j) dp[i + 1][j] = ans1 else: ans1 = dp[i + 1][j] if dp[i + 1][j + 1] == -1: ans2 = helper(i + 1, j + 1) dp[i + 1][j + 1] = ans2 else: ans2 = dp[i + 1][j + 1] if dp[i + 1][j - 1] == -1: ans3 = helper(i + 1, j - 1) dp[i + 1][j - 1] = ans3 else: ans3 = dp[i + 1][j - 1] return Matrix[i][j] + max(ans1, ans2, ans3) ans = 0 dp = [[(-1) for i in range(N + 1)] for j in range(N + 1)] for i in range(N): ans = max(ans, helper(0, i)) return ans
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER VAR VAR RETURN FUNC_CALL VAR STRING IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER VAR RETURN VAR
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, N, Matrix): adj = [-1, 0, 1] for row in range(1, N): for col in range(N): maximum = Matrix[row][col] for path in adj: if col + path >= 0 and col + path < N: maximum = max( Matrix[row - 1][col + path] + Matrix[row][col], maximum ) Matrix[row][col] = maximum res = 0 for pos in range(N): res = max(res, Matrix[N - 1][pos]) return res
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, n, mat): dp = [0] * n for i in range(n - 1, -1, -1): temp = [0] * n for j in range(n - 1, -1, -1): if i == n - 1: temp[j] = mat[i][j] continue left = down = right = mat[i][j] if j > 0: left += dp[j - 1] else: left = -1000000000.0 down += dp[j] if j < n - 1: right += dp[j + 1] else: right = -1000000000.0 temp[j] = max(left, down, right) dp[:] = temp[:] maxi = -1000000000.0 for i in range(n): maxi = max(maxi, dp[i]) return maxi
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def solve(self, i, j, n, mat, dp): if i >= n: return 0 if j >= n or j < 0: return 0 if dp[i][j] != -1: return dp[i][j] down = mat[i][j] + self.solve(i + 1, j, n, mat, dp) ldown = mat[i][j] + self.solve(i + 1, j - 1, n, mat, dp) rdown = mat[i][j] + self.solve(i + 1, j + 1, n, mat, dp) dp[i][j] = max(down, max(ldown, rdown)) return dp[i][j] def maximumPath(self, N, Matrix): dp = [[(-1) for i in range(N + 1)] for j in range(N + 1)] i = 0 j = 0 maxi = 0 for k in range(N): ans = self.solve(i, k, N, Matrix, dp) maxi = max(maxi, ans) return maxi
CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, N, matrix): m = len(matrix) n = len(matrix[0]) dp = [[(0) for _ in range(n)] for _ in range(m)] dp[0] = matrix[0] for i in range(m): for j in range(n): if i + 1 < m: dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + matrix[i + 1][j]) if i + 1 < m and j + 1 < n: dp[i + 1][j + 1] = max( dp[i + 1][j + 1], dp[i][j] + matrix[i + 1][j + 1] ) if i + 1 < m and j - 1 >= 0: dp[i + 1][j - 1] = max( dp[i + 1][j - 1], dp[i][j] + matrix[i + 1][j - 1] ) return max(dp[-1])
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def fun(self, i, j, m, arr, dp): if j < 0 or j >= m: return float("-inf") if i == 0: return arr[0][j] if dp[i][j] != -1: return dp[i][j] up = arr[i][j] + self.fun(i - 1, j, m, arr, dp) ld = arr[i][j] + self.fun(i - 1, j - 1, m, arr, dp) rd = arr[i][j] + self.fun(i - 1, j + 1, m, arr, dp) dp[i][j] = max(up, ld, rd) return dp[i][j] def maximumPath(self, n, arr): n, m = len(arr), len(arr[0]) dp = [[(-1) for i in range(m)] for j in range(n)] for j in range(m): dp[0][j] = arr[0][j] for i in range(1, n): for j in range(m): ld, rd = float("-inf"), float("-inf") up = arr[i][j] + dp[i - 1][j] if j - 1 >= 0: ld = arr[i][j] + dp[i - 1][j - 1] if j + 1 < m: rd = arr[i][j] + dp[i - 1][j + 1] dp[i][j] = max(up, ld, rd) maxi = dp[n - 1][0] for j in range(m): maxi = max(maxi, dp[n - 1][j]) return maxi
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR VAR RETURN FUNC_CALL VAR STRING IF VAR NUMBER RETURN VAR NUMBER VAR IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, N, Matrix): dp = [[(0) for i in range(N + 2)] for i in range(N + 1)] for i in range(1, N + 1): for j in range(1, N + 1): dp[i][j] = ( max(dp[i - 1][j - 1], dp[i - 1][j], dp[i - 1][j + 1]) + Matrix[i - 1][j - 1] ) m = -1000000000.0 for i in range(1, N + 1): m = max(dp[N][i], m) return m
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, N, Matrix): if N == 1: return Matrix[0][0] t = [[(0) for i in range(N + 1)] for _ in range(N + 1)] for i in range(1, N + 1): for j in range(1, N + 1): if j < N: t[i][j] = ( max(t[i - 1][j - 1], t[i - 1][j], t[i - 1][j + 1]) + Matrix[i - 1][j - 1] ) else: t[i][j] = max(t[i - 1][j - 1], t[i - 1][j]) + Matrix[i - 1][j - 1] gmax = 0 for i in range(N + 1): gmax = max(gmax, t[N][i]) return gmax
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def solve(self, r, c, arr, N, dp): if r >= N: return 0 if dp[r][c] != -1: return dp[r][c] MIN = arr[r][c] + self.solve(r + 1, c, arr, N, dp) if c - 1 >= 0: MIN = max(MIN, arr[r][c - 1] + self.solve(r + 1, c - 1, arr, N, dp)) if c + 1 < N: MIN = max(MIN, arr[r][c + 1] + self.solve(r + 1, c + 1, arr, N, dp)) dp[r][c] = MIN return MIN def maximumPath(self, N, Matrix): ans = 0 dp = [[(-1) for _ in i] for i in Matrix] for i in range(N): ans = max(ans, self.solve(0, i, Matrix, N, dp)) return ans
CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR RETURN VAR
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, N, Matrix): dp = [[(-1) for i in range(N)] for i in range(N)] def do(i, j): if i < 0 or j < 0 or i >= N or j >= N: return 0 if dp[i][j] != -1: return dp[i][j] dp[i][j] = Matrix[i][j] + max( do(i + 1, j - 1), do(i + 1, j), do(i + 1, j + 1) ) return dp[i][j] out = float("-inf") for i in range(N): out = max(out, do(0, i)) return out
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER VAR RETURN VAR
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def solve(self, Matrix, x, y, cord, dp): if 0 <= x <= len(Matrix) - 1 and 0 <= y <= len(Matrix[0]) - 1: if dp[x][y] != None: return dp[x][y] else: a = self.solve(Matrix, x + 1, y, cord, dp) b = self.solve(Matrix, x + 1, y - 1, cord, dp) c = self.solve(Matrix, x + 1, y + 1, cord, dp) dp[x][y] = Matrix[x][y] + max(a, b, c) return dp[x][y] else: return 0 def maximumPath(self, N, Matrix): start = Matrix[0] cord = [[1, 0], [1, -1], [1, 1]] col, row = len(Matrix[0]), len(Matrix) dp = [[None for i in range(col + 1)] for j in range(row + 1)] res = [] for i in range(len(start)): a = self.solve(Matrix, 0, i, cord, dp) res.append(a) return max(res)
CLASS_DEF FUNC_DEF IF NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR NONE RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NONE VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, N, Matrix): def solve(row, col, dp): if col < 0 or col >= N: return -1 if row == N - 1: return Matrix[row][col] if dp[row][col] != -1: return dp[row][col] dp[row][col] = Matrix[row][col] + max( solve(row + 1, col, dp), solve(row + 1, col - 1, dp), solve(row + 1, col + 1, dp), ) return dp[row][col] maxi = -1 dp = [[(-1) for i in range(N)] for j in range(N)] for col in range(N): pathsum = solve(0, col, dp) maxi = max(maxi, pathsum) return maxi
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER VAR VAR RETURN NUMBER IF VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def dfs(self, i, j, N, Matrix, dp): if i < 0 or i >= N or j < 0 or j >= N: return -1000000000.0 if i == N - 1: return Matrix[i][j] if dp[i][j] != -1: return dp[i][j] bottom = Matrix[i][j] + self.dfs(i + 1, j, N, Matrix, dp) bottomLeft = Matrix[i][j] + self.dfs(i + 1, j - 1, N, Matrix, dp) bottomRight = Matrix[i][j] + self.dfs(i + 1, j + 1, N, Matrix, dp) dp[i][j] = max(bottom, bottomLeft, bottomRight) return dp[i][j] def maximumPath(self, N, Matrix): maxSum = -1000000000.0 dp = [([-1] * N) for _ in range(N)] for j in range(N): maxSum = max(maxSum, self.dfs(0, j, N, Matrix, dp)) return maxSum
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR RETURN NUMBER IF VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR RETURN VAR
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, N, Matrix): n = len(Matrix) m = len(Matrix[0]) t = [[(0) for x in range(m)] for y in range(n)] for j in range(m): t[0][j] = Matrix[0][j] for i in range(1, n): for j in range(0, m): above = Matrix[i][j] + t[i - 1][j] left = Matrix[i][j] if j - 1 >= 0: left += t[i - 1][j - 1] else: left += -1000000000.0 right = Matrix[i][j] if j + 1 < m: right += t[i - 1][j + 1] else: right += -1000000000.0 t[i][j] = max(above, max(left, right)) mini = -100000 for i in range(m): mini = max(mini, t[n - 1][i]) return mini
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, N, Matrix): mv = [-1, 0, 1] for r in range(1, N): for c in range(N): total = 0 for j in mv: y = c + j if 0 <= y < N: total = max(total, Matrix[r - 1][y]) Matrix[r][c] += total ans = max(Matrix[-1]) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN VAR
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, N, Matrix): def manu(Matrix, dp): for i in range(N): dp[N - 1][i] = Matrix[N - 1][i] for i in range(N - 2, -1, -1): for j in range(N): x = Matrix[i][j] + dp[i + 1][j] if j - 1 >= 0: y = Matrix[i][j] + dp[i + 1][j - 1] else: y = -(10**9) if j + 1 < N: z = Matrix[i][j] + dp[i + 1][j + 1] else: z = -(10**9) dp[i][j] = max(x, max(y, z)) maxi = -(10**9) dp = [[(-1) for i in range(N)] for i in range(N)] manu(Matrix, dp) for i in range(N): maxi = max(maxi, dp[0][i]) return maxi
CLASS_DEF FUNC_DEF FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR RETURN VAR
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, n, matrix): dp = [[(-1) for i in range(n)] for j in range(n)] def solve(i, j, matrix): if j < 0 or j >= n: return -9999999 if i == 0: return matrix[0][j] if dp[i][j] != -1: return dp[i][j] up = matrix[i][j] + solve(i - 1, j, matrix) left = matrix[i][j] + solve(i - 1, j - 1, matrix) right = matrix[i][j] + solve(i - 1, j + 1, matrix) dp[i][j] = max(up, left, right) return dp[i][j] ans = -99999999 for j in range(n): temp = solve(n - 1, j, matrix) ans = max(ans, temp) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER VAR VAR RETURN NUMBER IF VAR NUMBER RETURN VAR NUMBER VAR IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, N, Matrix): def fun(i, j): if j < 0 or j >= len(matrix[0]): return 0 if i == 0: return matrix[0][j] if dp[i][j] != -1: return dp[i][j] l1 = matrix[i][j] + fun(i - 1, j) l2 = matrix[i][j] + fun(i - 1, j - 1) l3 = matrix[i][j] + fun(i - 1, j + 1) dp[i][j] = max(l1, max(l2, l3)) return dp[i][j] n = N matrix = Matrix maxi = 0 dp = [[(-1) for i in range(n)] for j in range(n)] for j in range(n - 1, -1, -1): re = fun(n - 1, j) maxi = max(maxi, re) return maxi
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR NUMBER VAR IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, n, matrix): dp = [[(0) for i in range(n)] for _ in range(n)] for i in range(n): dp[0][i] = matrix[0][i] for i in range(1, n): for j in range(n): up = 0 left = 0 right = 0 if i - 1 >= 0: up = dp[i - 1][j] if i - 1 >= 0 and j - 1 >= 0: left = dp[i - 1][j - 1] if i - 1 >= 0 and j + 1 < n: right = dp[i - 1][j + 1] dp[i][j] = matrix[i][j] + max(up, left, right) return max(dp[n - 1])
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def func(self, row, col, grid, dp): m = len(grid[0]) if row == 0 and col >= 0 and col < m: return grid[0][col] if col < 0 or col >= m or row < 0: return -int(1000000000.0) if dp[row][col] != -1: return dp[row][col] up = grid[row][col] + self.func(row - 1, col, grid, dp) leftDiag = grid[row][col] + self.func(row - 1, col - 1, grid, dp) rightDiag = grid[row][col] + self.func(row - 1, col + 1, grid, dp) dp[row][col] = max(max(up, leftDiag), rightDiag) return dp[row][col] def maximumPath(self, N, grid): maxi = -int(1000000000.0) dp = [[(-1) for j in range(N)] for i in range(N)] for j in range(N): m = self.func(N - 1, j, grid, dp) maxi = max(maxi, m) return maxi
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR RETURN VAR NUMBER VAR IF VAR NUMBER VAR VAR VAR NUMBER RETURN FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, N, Matrix): def max_path(matrix, i, j, n, dp): if i >= n or j >= n or j < 0: return 0 if dp[i][j] != -1: return dp[i][j] else: t1 = max_path(matrix, i + 1, j - 1, n, dp) t2 = max_path(matrix, i + 1, j, n, dp) t3 = max_path(matrix, i + 1, j + 1, n, dp) dp[i][j] = matrix[i][j] + max(t1, t2, t3) return dp[i][j] m = float("-inf") dp = [[(-1) for j in range(N)] for i in range(N)] for j in range(N): temp = max_path(Matrix, 0, j, N, dp) for j in range(N): m = max(m, dp[0][j]) return m
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR RETURN VAR
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
import sys class Solution: def maximumPath(self, n, grid): import sys dp = [[(-sys.maxsize) for j in range(n)] for i in range(n)] max_ = -sys.maxsize for i in range(n): for j in range(n): if i == 0: dp[i][j] = grid[i][j] continue ld = rd = -sys.maxsize up = grid[i][j] + dp[i - 1][j] if j > 0: ld = grid[i][j] + dp[i - 1][j - 1] if j < n - 1: rd = grid[i][j] + dp[i - 1][j + 1] dp[i][j] = max(dp[i][j], max(up, max(ld, rd))) for j in range(n): max_ = max(max_, dp[n - 1][j]) return max_
IMPORT CLASS_DEF FUNC_DEF IMPORT ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, N, Matrix): dp = [] for i in range(N): temp = [] for j in range(N): temp.append(0) dp.append(temp) for i in range(N): dp[0][i] = Matrix[0][i] for i in range(1, N): for j in range(N): if j == 0: dp[i][j] = max(dp[i - 1][j], dp[i - 1][j + 1]) + Matrix[i][j] elif j + 1 == N: dp[i][j] = max(dp[i - 1][j - 1], dp[i - 1][j]) + Matrix[i][j] else: dp[i][j] = ( max(dp[i - 1][j - 1], dp[i - 1][j], dp[i - 1][j + 1]) + Matrix[i][j] ) return max(dp[-1])
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, N, Matrix): dp = [[(0) for i in range(N)] for j in range(N)] for i in range(N): dp[N - 1][i] = Matrix[N - 1][i] for i in range(N - 2, -1, -1): for j in range(N): nxtSteps = [dp[i + 1][j]] if j < N - 1: nxtSteps.append(dp[i + 1][j + 1]) if j > 0: nxtSteps.append(dp[i + 1][j - 1]) dp[i][j] = max(nxtSteps) + Matrix[i][j] return max(dp[0])
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, N, Matrix): new_matrix = [Matrix[0]] for i in range(1, N): row = [0] * N for j in range(N): if j == 0: row[j] = Matrix[i][j] + max( new_matrix[-1][j], new_matrix[-1][j + 1] ) elif j == N - 1: row[j] = Matrix[i][j] + max( new_matrix[-1][j], new_matrix[-1][j - 1] ) else: row[j] = Matrix[i][j] + max( new_matrix[-1][j], new_matrix[-1][j + 1], new_matrix[-1][j - 1] ) new_matrix.append(row) return max(new_matrix[-1])
CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR NUMBER
Given a NxN matrix of positive integers. There are only three possible moves from a cell Matrix[r][c]. Matrix [r+1] [c] Matrix [r+1] [c-1] Matrix [r+1] [c+1] Starting from any column in row 0 return the largest sum of any of the paths up to row N-1. NOTE: We can start from any column in zeroth row and can end at any column in (N-1)th row. Example 1: Input: N = 2 Matrix = {{348, 391}, {618, 193}} Output: 1009 Explaination: The best path is 391 -> 618. It gives the sum = 1009. Example 2: Input: N = 2 Matrix = {{2, 2}, {2, 2}} Output: 4 Explaination: No matter which path is chosen, the output is 4. Your Task: You do not need to read input or print anything. Your task is to complete the function maximumPath() which takes the size N and the Matrix as input parameters and returns the highest maximum path sum. Expected Time Complexity: O(N*N) Expected Auxiliary Space: O(N*N) Constraints: 1 ≤ N ≤ 500 1 ≤ Matrix[i][j] ≤ 1000
class Solution: def maximumPath(self, N, Matrix): m = len(Matrix) n = len(Matrix[0]) dp = [[(0) for i in range(n)] for i in range(m)] for row in range(m - 1, -1, -1): for col in range(n - 1, -1, -1): if row == m - 1: right = 0 else: right = dp[row + 1][col] if row == m - 1 or col == 0: rightback = 0 else: rightback = dp[row + 1][col - 1] if row == m - 1 or col == n - 1: rightfront = 0 else: rightfront = dp[row + 1][col + 1] dp[row][col] = Matrix[row][col] + max(right, rightback, rightfront) mx = 0 for i in range(m): if dp[0][i] > mx: mx = dp[0][i] return mx
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR RETURN VAR
Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.   Example 1: Input: s = "(()" Output: 2 Explanation: The longest valid parentheses substring is "()". Example 2: Input: s = ")()())" Output: 4 Explanation: The longest valid parentheses substring is "()()". Example 3: Input: s = "" Output: 0   Constraints: 0 <= s.length <= 3 * 104 s[i] is '(', or ')'.
class Solution: def longestValidParentheses(self, s: str) -> int: left = 0 right = 0 res = 0 for c in s: if c == "(": left += 1 if c == ")": right += 1 if left == right: res = max(res, left + right) if right > left: left = 0 right = 0 left = 0 right = 0 for i in range(len(s) - 1, -1, -1): c = s[i] if c == "(": left += 1 if c == ")": right += 1 if left == right: res = max(res, left + right) if left > right: left = 0 right = 0 return res
CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER RETURN VAR VAR
Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.   Example 1: Input: s = "(()" Output: 2 Explanation: The longest valid parentheses substring is "()". Example 2: Input: s = ")()())" Output: 4 Explanation: The longest valid parentheses substring is "()()". Example 3: Input: s = "" Output: 0   Constraints: 0 <= s.length <= 3 * 104 s[i] is '(', or ')'.
class Solution: def longestValidParentheses(self, s: str) -> int: memo = {} n = len(s) def dp(i): if i <= 0: return 0 if i in memo: return memo[i] if (s[i - 1], s[i]) == ("(", ")"): memo[i] = dp(i - 2) + 2 elif ( (s[i - 1], s[i]) == (")", ")") and i - dp(i - 1) - 1 >= 0 and s[i - dp(i - 1) - 1] == "(" ): memo[i] = dp(i - 1) + 2 + dp(i - dp(i - 1) - 2) else: memo[i] = 0 return memo[i] ret = 0 for i in range(n - 1, 0, -1): ret = max(ret, dp(i)) return ret
CLASS_DEF FUNC_DEF VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR STRING STRING ASSIGN VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR STRING STRING BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER STRING ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given a non-empty array containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal. Note: Each of the array element will not exceed 100. The array size will not exceed 200. Example 1: Input: [1, 5, 11, 5] Output: true Explanation: The array can be partitioned as [1, 5, 5] and [11]. Example 2: Input: [1, 2, 3, 5] Output: false Explanation: The array cannot be partitioned into equal sum subsets.
class Solution: def canPartition(self, nums): if len(nums) < 2: return False s = sum(nums) if s % 2 != 0: return False s = s // 2 nums.sort() mem = dict() return self.dp(nums, 0, s, mem) def dp(self, nums, start, s, mem): if s == 0: return True if start >= len(nums): return False state = mem.get((start, s), None) if state is not None: return state res = False if nums[start] > s: res = False else: res = self.dp(nums, start + 1, s - nums[start], mem) or self.dp( nums, start + 1, s, mem ) mem[start, s] = res return res
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NONE IF VAR NONE RETURN VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR
Given a non-empty array containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal. Note: Each of the array element will not exceed 100. The array size will not exceed 200. Example 1: Input: [1, 5, 11, 5] Output: true Explanation: The array can be partitioned as [1, 5, 5] and [11]. Example 2: Input: [1, 2, 3, 5] Output: false Explanation: The array cannot be partitioned into equal sum subsets.
class Solution: def canPartition(self, nums): nums.sort(reverse=True) print(nums) numsum = sum(nums) if numsum % 2 != 0: return False halfsum = numsum // 2 t_sum = nums[0] if t_sum > halfsum: return False elif t_sum == halfsum: return True llen = len(nums) for startpos in range(1, llen): curpos = startpos while t_sum < halfsum and curpos < llen: if t_sum + nums[curpos] > halfsum: curpos += 1 elif t_sum + nums[curpos] == halfsum: return True else: t_sum += nums[curpos] curpos += 1 pass t_sum = nums[0] return False
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR RETURN NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER
Given a non-empty array containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal. Note: Each of the array element will not exceed 100. The array size will not exceed 200. Example 1: Input: [1, 5, 11, 5] Output: true Explanation: The array can be partitioned as [1, 5, 5] and [11]. Example 2: Input: [1, 2, 3, 5] Output: false Explanation: The array cannot be partitioned into equal sum subsets.
class Solution: def canPartition(self, nums): s = 0 bits = 1 for num in nums: s += num bits |= bits << num return not s & 1 and bits >> (s >> 1) & 1 == 1
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR BIN_OP VAR VAR RETURN BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER
Given a non-empty array containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal. Note: Each of the array element will not exceed 100. The array size will not exceed 200. Example 1: Input: [1, 5, 11, 5] Output: true Explanation: The array can be partitioned as [1, 5, 5] and [11]. Example 2: Input: [1, 2, 3, 5] Output: false Explanation: The array cannot be partitioned into equal sum subsets.
class Solution: def search(self, nums, pos, target, seen): if (pos, target) in seen: return False if target == 0: return True if pos >= len(nums) or nums[pos] > target: return False if self.search(nums, pos + 1, target - nums[pos], seen): return True if self.search(nums, pos + 1, target, seen): return True seen.add((pos, target)) return False def canPartition(self, nums): s = sum(nums) if s % 2 == 1: return False target = s // 2 nums = sorted(nums) return self.search(nums, 0, target, set())
CLASS_DEF FUNC_DEF IF VAR VAR VAR RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR
Given a non-empty array containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal. Note: Each of the array element will not exceed 100. The array size will not exceed 200. Example 1: Input: [1, 5, 11, 5] Output: true Explanation: The array can be partitioned as [1, 5, 5] and [11]. Example 2: Input: [1, 2, 3, 5] Output: false Explanation: The array cannot be partitioned into equal sum subsets.
class Solution: def canPartition(self, nums): _sum = sum(nums) div, mod = divmod(_sum, 2) if mod or max(nums) > div: return False nums.sort(reverse=True) target = [div] * 2 return self.dfs(nums, 0, target) def dfs(self, nums, index, target): for i in range(2): if target[i] >= nums[index]: target[i] -= nums[index] if target[i] == 0 or self.dfs(nums, index + 1, target): return True target[i] += nums[index] return False
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN NUMBER VAR VAR VAR VAR RETURN NUMBER
Given a non-empty array containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal. Note: Each of the array element will not exceed 100. The array size will not exceed 200. Example 1: Input: [1, 5, 11, 5] Output: true Explanation: The array can be partitioned as [1, 5, 5] and [11]. Example 2: Input: [1, 2, 3, 5] Output: false Explanation: The array cannot be partitioned into equal sum subsets.
class Solution: def canPartition(self, nums): _sum = sum(nums) div, mod = divmod(_sum, 2) if mod != 0: return False target = [div] * 2 self._len = len(nums) nums.sort(reverse=True) def dfs(index, target): if index == self._len: return True num = nums[index] for i in range(2): if target[i] >= num: target[i] -= num if dfs(index + 1, target): return True target[i] += num return False return dfs(0, target)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN NUMBER VAR VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR NUMBER VAR
Given a non-empty array containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal. Note: Each of the array element will not exceed 100. The array size will not exceed 200. Example 1: Input: [1, 5, 11, 5] Output: true Explanation: The array can be partitioned as [1, 5, 5] and [11]. Example 2: Input: [1, 2, 3, 5] Output: false Explanation: The array cannot be partitioned into equal sum subsets.
class Solution: def canPartition(self, nums): target = sum(nums) if target % 2 == 1: return False target = target // 2 if any([x for x in nums if x > target]): return False nums.sort() def check(nums, target): if target in nums: return True if not nums or nums[0] > target: return False res = False tag = "@" for i in range(len(nums)): if tag != nums[i]: tag = nums[i] res = res | check(nums[:i] + nums[i + 1 :], target - nums[i]) if res: return True return res return check(nums, target)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR NUMBER VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR IF VAR RETURN NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR VAR
Given a non-empty array containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal. Note: Each of the array element will not exceed 100. The array size will not exceed 200. Example 1: Input: [1, 5, 11, 5] Output: true Explanation: The array can be partitioned as [1, 5, 5] and [11]. Example 2: Input: [1, 2, 3, 5] Output: false Explanation: The array cannot be partitioned into equal sum subsets.
class Solution: def canPartition(self, nums): sum_ = sum(nums) if sum_ % 2 == 0: target = sum_ / 2 def myFun(t, nu): nu.sort() nu.reverse() for index, item in enumerate(nu): if item == t: return True elif item > t: return False else: nun = nu.copy() del nun[index] if myFun(t - item, nun): return True elif index == len(nu) - 1: return False return myFun(target, nums) return False
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR RETURN NUMBER
Given a non-empty array containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal. Note: Each of the array element will not exceed 100. The array size will not exceed 200. Example 1: Input: [1, 5, 11, 5] Output: true Explanation: The array can be partitioned as [1, 5, 5] and [11]. Example 2: Input: [1, 2, 3, 5] Output: false Explanation: The array cannot be partitioned into equal sum subsets.
class Solution: def canPartition(self, nums): s = sum(nums) if s & 1: return False target = s // 2 cnt = collections.Counter(nums) return self.helper(target, cnt) def helper(self, target, cnt): if target == 0: return True if target < 0: return False for item in sorted(list(cnt.keys()), reverse=True): if cnt[item] == 0: continue cnt[item] -= 1 if self.helper(target - item, cnt): return True cnt[item] += 1 return False
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN NUMBER VAR VAR NUMBER RETURN NUMBER
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
class Solution: def numFactoredBinaryTrees(self, A: List[int]) -> int: A.sort() numToIdx = {x: i for i, x in enumerate(A)} dp = [(1) for _ in range(len(A))] MOD = 10**9 + 7 totalSum = 0 for i, x in enumerate(A): for j in range(i): y = A[j] if x % y == 0 and x // y in numToIdx: dp[i] += dp[j] * dp[numToIdx[x // y]] totalSum = (totalSum + dp[i]) % MOD return totalSum
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR RETURN VAR VAR
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
class Solution: def numFactoredBinaryTrees(self, A: List[int]) -> int: sorted_A = sorted(A) dp = dict() result = 0 for i in range(len(sorted_A)): cur_total = 0 for j in range(i + 1): cur_val = sorted_A[i] prev_val = sorted_A[j] if i == j: dp[cur_val] = cur_total + 1 result += cur_total + 1 elif cur_val / prev_val in dp: cur_total += dp[cur_val / prev_val] * dp[prev_val] return result % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree using these integers and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of it's children. How many binary trees can we make?  Return the answer modulo 10 ** 9 + 7. Example 1: Input: A = [2, 4] Output: 3 Explanation: We can make these trees: [2], [4], [4, 2, 2] Example 2: Input: A = [2, 4, 5, 10] Output: 7 Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].   Note: 1 <= A.length <= 1000. 2 <= A[i] <= 10 ^ 9.
class Solution: def numFactoredBinaryTrees(self, A: List[int]) -> int: A.sort() dp = {} res = 0 for i in range(len(A)): dp[A[i]] = 1 for j in range(i): if A[j] * A[j] == A[i]: dp[A[i]] += dp[A[j]] * dp[A[j]] dp[A[i]] %= 1000000007 elif A[j] * A[j] > A[i] and A[i] % A[j] == 0: dp[A[i]] += dp[A[j]] * dp.get(A[i] // A[j], 0) * 2 dp[A[i]] %= 1000000007 res = sum([v for _, v in dp.items()]) res %= 1000000007 return res
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER RETURN VAR VAR