Skip to content
Open

DP1 #1996

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
41 changes: 41 additions & 0 deletions HouseRobber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Time Complexity : O(n) where n is the number of houses
// Space Complexity : O(1) as we are using only two variables
// 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
// As discussed in class, Greedy or selective approach will not work here,
// as we might end up with a local optimal solution.
// We have to go with exhaustive approach,
// where we will try all the combinations of houses to make up the amount.
// But it will lead to exponential time complexity,
// so we will use dynamic programming to optimize it.
// The input variable is nums, which is changing, so we will create a dp array of size n
// where n is the number of houses.
// For every house, if we choose to rob it,
// we will take the value of that house and add it to the value of the house two steps
// back, as we cannot rob adjacent houses.
// If we choose not to rob it, we will take the value from the previous house.
// Lastly, we will return the value at the last index of the dp array, which will give us the maximum amount we can rob.


public class HouseRobber {
class Solution {
public int rob(int[] nums) {
if (nums.length < 2) return nums[0];

int n = nums.length;
int[] dp = new int[n];

dp[0] = nums[0];
dp[1] = Math.max(nums[0], nums[1]);

for (int i = 2; i < n; i++) {
dp[i] = Math.max(nums[i] + dp[i - 2], dp[i - 1]);
}

return dp[n - 1];
}
}
}
55 changes: 51 additions & 4 deletions Sample.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,54 @@
// Time Complexity :
// Space Complexity :
// Did this code successfully run on Leetcode :
// Any problem you faced while coding this :
// Time Complexity : O(m*n) where m is the number of coins and n is the amount
// Space Complexity : o(m*n) where m is the number of coins and n is the amount
// Did this code successfully run on Leetcode :yes
// Any problem you faced while coding this : not exactly, but I had to be careful
// with the initialization of the dp array to avoid overflow issues.


// Your code here along with comments explaining your approach
// As discussed in class, Greedy or selective approach will not work here,
// as we might end up with a local optimal solution.
// We have to go with exhaustive approach,
// where we will try all the combinations of coins to make up the amount.
// But it will lead to exponential time complexity,
// so we will use dynamic programming to optimize it.
// The input variables are coins and amount,which are changing, so we will create a dp array of size (m+1) x (n+1) where m is the number of coins and n is the amount.
// For every amount, if 0 - not choose we will take the value from the previous row,
// if we choose the coin, we will take 1 + value from the same row and column - coin value.
// Lastly, we will return the value at the bottom right corner of the dp array, if it is still the initialized value, we will return -1,
// otherwise we will return that value.
class Solution {
public int coinChange(int[] coins, int amount) {

int m = coins.length;
int n = amount;

int[][] dp = new int[m + 1][n + 1];

dp[0][0] = 0;

// Initialize top row with a large value
for (int j = 1; j <= n; j++) {
dp[0][j] = Integer.MAX_VALUE - 10; // avoid overflow
}

for (int i = 1; i <= m; i++) {
for (int j = 0; j <= n; j++) {

if (j < coins[i - 1]) {
dp[i][j] = dp[i - 1][j];
} else {
dp[i][j] = Math.min(
dp[i - 1][j],
1 + dp[i][j - coins[i - 1]]
);
}
}
}

int res = dp[m][n];

if (res == Integer.MAX_VALUE - 10) return -1;
return res;
}
}