diff --git a/Minimum Falling Path Sum.py b/Minimum Falling Path Sum.py new file mode 100644 index 00000000..7acf7a6f --- /dev/null +++ b/Minimum Falling Path Sum.py @@ -0,0 +1,55 @@ +# Time Complexity --> O(n*n) where n*n are the dimensions of matrix +# Space Complexity --> O(n) +class Solution: + def minFallingPathSum(self, matrix: List[List[int]]) -> int: + n = len(matrix) + if n==1: + return matrix[0][0] + + dp = [0 for i in range(n)] + for i in range(n): + left = 0 + for j in range(n): + temp = dp[j] + if j==0: + dp[j] = matrix[i][j] + min(dp[j], dp[j+1]) + elif j==n-1: + dp[j] = matrix[i][j] + min(left, dp[j]) + else: + dp[j] = matrix[i][j] + min(left, dp[j], dp[j+1]) + left = temp + + return min(dp) + + + +''' +# Time Complexity --> O(n*n) where n*n are the dimensions of matrix +# Space Complexity --> O(n*n) +class Solution: + def minFallingPathSum(self, matrix: List[List[int]]) -> int: + + re = float('inf') + n = len(matrix) + self.memo = [[float('inf')]*(n) for i in range(n)] + for i in range(n): + re = min(re, self.helper(matrix, 0, i)) + return re + + + def helper(self, matrix, row, col): + # base + if row==len(matrix): + return 0 + if self.memo[row][col]!=float('inf'): + return self.memo[row][col] + # logic + if col==0: + case = matrix[row][col] + min(self.helper(matrix, row+1, col), self.helper(matrix, row+1, col+1)) + elif col==len(matrix)-1: + case = matrix[row][col] + min(self.helper(matrix, row+1, col-1), self.helper(matrix, row+1, col)) + else: + case = matrix[row][col]+min(self.helper(matrix, row+1, col-1), self.helper(matrix, row+1, col), self.helper(matrix, row+1, col+1)) + self.memo[row][col] = case + return case +''' diff --git a/delete and earn.py b/delete and earn.py new file mode 100644 index 00000000..5783d45d --- /dev/null +++ b/delete and earn.py @@ -0,0 +1,14 @@ +# Time Complexity --> O(n) where n is the max value available in the input list +# Space Complexity --> O(n) +# Approach --> creating an array where the indices would represent the number available in nums list. The values would be the sum of all its occurrences +class Solution: + def deleteAndEarn(self, nums: List[int]) -> int: + arr = [0]*(max(nums)+1) + for i in range(len(nums)): + arr[nums[i]] = arr[nums[i]]+nums[i] + + + for i in range(2, len(arr)): + arr[i] = max(arr[i-1], arr[i]+arr[i-2]) + return arr[-1] +