Skip to content
Open
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
23 changes: 23 additions & 0 deletions Delete_and_earn.java
Original file line number Diff line number Diff line change
@@ -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];
}
}
30 changes: 30 additions & 0 deletions Minimum_Falling_Path_Sum.java
Original file line number Diff line number Diff line change
@@ -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;j++){
min=Math.min(min,helper(matrix,0,j));
}
return min;
}
private int helper(int[][] matrix, int i, int j){
int n = matrix.length;
// base
if(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];
}
}