Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completed DP-3; Please review #213

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 39 additions & 0 deletions DeleteEarn_LC740.java
@@ -0,0 +1,39 @@
/**
* Sorting will still make it O(n log n) and so we choose to maintain index array in order to keep track of
* elements before or after an element(-1/+1)
* Once radix array is formed, at every index, we have an option to choose or not choose it
* Solve for getting max rewards
*/

// Time Complexity: O (max (n, maxElement)); n=length of array, maxElement=maximum value in nums
// Space Complexity: O (M); M=maxElement in nums
// Did this code successfully run on Leetcode : Yes
public class DeleteEarn_LC740 {
public int deleteAndEarn(int[] nums) {
// Handle empty input
if (nums == null || nums.length == 0)
return 0;

// Initialize & construct radix array
int[] radixA = new int[getMaxElement(nums) + 1];
for (int num : nums)
radixA[num] += num;

// Problem reduced to House Robber
int choose = 0, noChoose = 0;
for (int num : radixA) {
int temp = Math.max(choose, noChoose);
choose = noChoose + num; // Previous shouldn't be chosen
noChoose = temp;
}
return Math.max(choose, noChoose);
}

public int getMaxElement(int[] nums) {
int max = Integer.MIN_VALUE;
for (int num : nums)
if (num > max)
max = num;
return max;
}
}
38 changes: 38 additions & 0 deletions MinFallingPath_LC931.java
@@ -0,0 +1,38 @@
/**
* For a minimum path, choose the minimum value out to 3 options(edges 2 options) at
* each row, keep calculating and storing in A.
* We assume that it's okay to modify A, otherwise copy array will be made
* The minimum value from last row would be the answer
**/

// Time Complexity: O (N*N) : N: number of rows in square matrix
// Space Complexity: O (1)
// Did this code successfully run on Leetcode : Yes
public class MinFallingPath_LC931 {
public int minFallingPathSum(int[][] A) {
// Handle empty input
if (A == null || A.length == 0 || A[0].length == 0)
return -1;

// Iterate and calculate min at each row and column
for (int i = 1; i < A.length; i++) {
for (int j = 0; j < A[0].length; j++) {
if (j == 0) // Corner case 1
A[i][j] += Math.min(A[i - 1][j], A[i - 1][j + 1]);
else if (j == A[0].length - 1) // Corner case 2
A[i][j] += Math.min(A[i - 1][j - 1], A[i - 1][j]);
else // Can acess all 3 choices
A[i][j] += Math.min(A[i - 1][j + 1], Math.min(A[i - 1][j - 1], A[i - 1][j]));
}
}
return getMin(A[A.length - 1]);
}

public int getMin(int[] lastA) {
int min = Integer.MAX_VALUE;
for (int num : lastA)
if (num < min)
min = num;
return min;
}
}