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
37 changes: 37 additions & 0 deletions DeleteAndEarn.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Time Complexity : O(N+k)
// Space Complexity : o(N)
// 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
// Find the maximum value in nums so we can create an array of size max + 1
// arr[i] stores the total points earned by taking all occurrences of number i
// House Robber logic: prev represents the best answer up to i - 2 and curr represents the best answer up to i - 1
class DeleteAndEarn{
public int deleteAndEarn(int[] nums) {

int max =Integer.MIN_VALUE;
for(int num : nums){
max = Math.max(max, num);
}

int []arr = new int[max+1];

for(int num : nums){
arr[num] += num;
}

int prev = arr[0];
int curr = Math.max(prev, arr[1]);

for(int i = 2; i < arr.length; i++){
int temp = curr;
curr = Math.max(curr, prev+arr[i]);
prev= temp;
}

return curr;

}
}
26 changes: 26 additions & 0 deletions MinFallingPathSum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class MinFallingPathSum {
public int minFallingPathSum(int[][] matrix) {
int dp[] = new int[matrix.length + 1];
for (int row = matrix.length - 1; row >= 0; row--) {
int currentRow[] = new int[matrix.length + 1];
for (int col = 0; col < matrix.length; col++) {
if (col == 0) {
currentRow[col] =
Math.min(dp[col], dp[col + 1]) + matrix[row][col];
} else if (col == matrix.length - 1) {
currentRow[col] =
Math.min(dp[col], dp[col - 1]) + matrix[row][col];
} else {
currentRow[col] = Math.min(dp[col],
Math.min(dp[col + 1], dp[col - 1])) + matrix[row][col];
}
}
dp = currentRow;
}
int minFallingSum = Integer.MAX_VALUE;
for (int startCol = 0; startCol < matrix.length; startCol++) {
minFallingSum = Math.min(minFallingSum, dp[startCol]);
}
return minFallingSum;
}
}
7 changes: 0 additions & 7 deletions Sample.java

This file was deleted.