From 94f8249f9d18d37f2f7d83aca6bd634498040d01 Mon Sep 17 00:00:00 2001 From: Hriday-A Date: Mon, 20 Apr 2026 17:23:06 -0500 Subject: [PATCH] Completed DP-3 Assignment --- Delete_and_earn.java | 23 +++++++++++++++++++++++ Minimum_Falling_Path_Sum.java | 30 ++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 Delete_and_earn.java create mode 100644 Minimum_Falling_Path_Sum.java diff --git a/Delete_and_earn.java b/Delete_and_earn.java new file mode 100644 index 00000000..f03c6814 --- /dev/null +++ b/Delete_and_earn.java @@ -0,0 +1,23 @@ +// Time Complexity : O(n) +// Space Complexity : O(n) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No +class Solution { + public int deleteAndEarn(int[] nums) { + int max=0; + for(int num: nums){ + max = Math.max(max, num); + } + int[] arr= new int[max+1]; + for(int num:nums){ + arr[num]+=num; + } + int[] dp = new int[arr.length+1]; + dp[0]=0; + dp[1] = Math.max(arr[0], arr[1]); + for(int i=2;i<=max;i++){ + dp[i] = Math.max(dp[i-1],arr[i]+dp[i-2]); + } + return dp[max]; + } +} \ No newline at end of file diff --git a/Minimum_Falling_Path_Sum.java b/Minimum_Falling_Path_Sum.java new file mode 100644 index 00000000..92ea54cc --- /dev/null +++ b/Minimum_Falling_Path_Sum.java @@ -0,0 +1,30 @@ +// Time Complexity :O(N^2) +// Space Complexity :O(N^2) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : Yes I am facing issue in implementing DP for this question. +class Solution { + Integer [][] memo; + public int minFallingPathSum(int[][] matrix) { + if(matrix==null || matrix.length==0) return 0; + int min = Integer.MAX_VALUE; + int n = matrix.length; + this.memo= new Integer[n][n]; + for(int j=0;j=n) return Integer.MAX_VALUE -10; + if (i==n) return 0; + if(memo[i][j]!= null) return memo[i][j]; + // logic + int down= helper(matrix,i+1,j); + int left = helper(matrix,i+1,j-1); + int right = helper(matrix,i+1,j+1); + memo[i][j] = matrix[i][j] + Math.min(down,Math.min(left,right)); + return memo[i][j]; + } +} \ No newline at end of file