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
40 changes: 40 additions & 0 deletions Coinchange.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Time Complexity : O(m*n)
// 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
/*
* Take a dp array with sizes equal to one greater than number of coins with row and one greater than amount
* as columns. Initial row would be filled with infinite value, but in ideal situation, to avoid integer
* overflow, we intialize just with amount+1. Now, we try to fill the matrix iteratively by considering
* choose and no choose scenarios. If no choose, where the amount is less than number of coins, the cell
* value comes from the value of cell just above row. If choose, we take min value between no choose and
* choose, where we obtain the value from the cell in same row and column of current amount and denomination
* of coin.If the last cell still remains with amount+1, then we return -1, if not, we return that value.
* */
class Solution {
public int coinChange(int[] coins, int amount) {
int m = coins.length;
int n = amount;

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

//initialize with some infinite value and make sure to avoid Integer overflow
for(int j = 1 ; j <= n ; j++)
dp[j] = amount + 1;

for(int i = 1 ; i <= m ; i++) {
for(int j = 1 ; j <= n ; j++) {
if(j < coins[i - 1]) //no choose case
dp[j] = dp[j]; // directly comes from above column
else {
//find min value of choose(same row but column comes from amount - coin denomination) and no choose
// In choose situation, we need to add extra 1, because we are choosing a coin
dp[j] = Math.min(dp[j] , 1 + dp[j - coins[i - 1]]);
}
}
}
return dp[n] == amount + 1 ? -1 : dp[n];
}
}
42 changes: 42 additions & 0 deletions HouseRobber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Time Complexity : O(n)
// Space Complexity : O(1)
// 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 take a dp 1-D array to to compute the value at index i by considering the max of i - 1 and i -2
indices by relating choose and no choose cases. If we do it recursively, is 2^n TC and contains lot of
repetitive subproblems. So we optimize it with DP. We can also further optimize by removing dp array and
use only 2 variables like current and previous as we are not relying on other values excepr i-1, i-2.
*/
class Solution {
public int rob(int[] nums) {
if(nums.length == 1)
return nums[0];
int m = nums.length;
//Approach 1
// int[] dp = new int[m + 1];

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

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

// return dp[m - 1];

//Approach 2 for optimized space
int prev = nums[0];
int curr = Math.max(nums[0], nums[1]);

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

}
}