Skip to content
Open

DP 3 #1553

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions DeleteandEarn.java
Original file line number Diff line number Diff line change
@@ -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];
}
}
66 changes: 66 additions & 0 deletions MinimumFallingSum.java
Original file line number Diff line number Diff line change
@@ -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;
}
}