From 4e9fda01a987a848140ffe60781bcdeb25420d42 Mon Sep 17 00:00:00 2001 From: MeghaN28 Date: Sat, 18 Jul 2026 19:18:30 -0700 Subject: [PATCH] DP 3 --- DeleteandEarn.java | 51 ++++++++++++++++++++++++++++++++ MinimumFallingSum.java | 66 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 DeleteandEarn.java create mode 100644 MinimumFallingSum.java diff --git a/DeleteandEarn.java b/DeleteandEarn.java new file mode 100644 index 00000000..f174cc9e --- /dev/null +++ b/DeleteandEarn.java @@ -0,0 +1,51 @@ +// Time Complexity : O(n) where n is the maximum number in the input array. We iterate through the input array to find the maximum number and then fill the dp array based on the values in arr. +// Space Complexity : O(n) where n is the maximum number in the input array. We use an additional array arr to store the sum of values for each number and a dp array to store the maximum points that can be earned up to each number. +// Did this code successfully run on Leetcode : YES +// Any problem you faced while coding this : NO + + +// Your code here along with comments explaining your approach +// Greedy will not work, we need to go exhaustive, but it will give time limit +// So we need to do tabulation / memoization +// We can use a dp array to store the maximum points that can be earned up to each number. +// We first find the maximum number in the input array to determine the size of the dp array. +// We then create an array arr where arr[i] represents the total points that can be earned by deleting all instances of number i in the input array. +// We then fill the dp array using the following recurrence relation: +// dp[i] = max(dp[i - 1], arr[i] + dp[i - 2]) +// This means that for each number i, we can either choose to delete it and earn the points from arr[i] plus the maximum points earned up to i-2, or we can choose to skip it and take the maximum points earned up to i-1. +// We return dp[max] as the final answer, which represents the maximum points that can be earned by deleting numbers from + + +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[max + 1]; + + dp[0] = arr[0]; + + if (max >= 1) { + 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/MinimumFallingSum.java b/MinimumFallingSum.java new file mode 100644 index 00000000..7e187d26 --- /dev/null +++ b/MinimumFallingSum.java @@ -0,0 +1,66 @@ +// Time Complexity : O(n*N) where n is the number of rows (or columns) in the input matrix. We iterate through each cell in the matrix to fill the dp array. +// Space Complexity : O(n*N) where n is the number of rows (or columns) in the input matrix. We use an additional 2D array dp to store the minimum falling path sum up to each cell. +// Did this code successfully run on Leetcode : YES +// Any problem you faced while coding this : NO + + +// Your code here along with comments explaining your approach +// We can use dynamic programming to solve this problem. +// We create a 2D dp array where dp[i][j] represents the minimum falling path sum to reach cell (i, j). +// We initialize the first row of the dp array with the values from the first row of the input matrix. +// Then, for each subsequent row, we calculate the minimum falling path sum for each cell by considering the three possible cells from which we can come: directly above (i-1, j), diagonally left (i-1, j-1), and diagonally right (i-1, j+1). +// Finally, we find the minimum value in the last row of the dp array, which represents the minimum falling path sum from top to bottom. + + +class Solution { + + public int minFallingPathSum(int[][] matrix) { + + int n = matrix.length; + + int min = Integer.MAX_VALUE; + + int[][] dp = new int[n][n]; + + // First row initialization + for (int j = 0; j < n; j++) { + dp[0][j] = matrix[0][j]; + } + + // Fill DP table + for (int i = 1; i < n; i++) { + + for (int j = 0; j < n; j++) { + + if (j == 0) { + + dp[i][j] = matrix[i][j] + + Math.min(dp[i - 1][j], + dp[i - 1][j + 1]); + + } else if (j == n - 1) { + + dp[i][j] = matrix[i][j] + + Math.min(dp[i - 1][j - 1], + dp[i - 1][j]); + + } else { + + dp[i][j] = matrix[i][j] + + Math.min( + Math.min(dp[i - 1][j - 1], + dp[i - 1][j]), + dp[i - 1][j + 1] + ); + } + } + } + + // Find minimum in last row + for (int j = 0; j < n; j++) { + min = Math.min(min, dp[n - 1][j]); + } + + return min; + } +} \ No newline at end of file