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
110 changes: 110 additions & 0 deletions Leetcode_198.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
//Way:1
//Recurssion-- choose or no choose
//TC: 2^(O(n))
class Solution {
public int rob(int[] nums) {
return helper(nums,0);

}

private int helper (int[] nums, int idx){
//base case
if(idx>=nums.length) return 0;

//logic
//no choose
int case1=helper(nums,idx+1);
//choose
int case2=nums[idx]+helper(nums,idx+2);

return Math.max(case1,case2);

}
}

//Way:2
//Recurssion-- choose or no choose -- causing time limit exceeded
//So, going with memoization-- remembering the result
//TC: O(n)
class Solution {
public int rob(int[] nums) {
int[] memo = new int[nums.length];
return helper(nums,0,memo);

}

private int helper (int[] nums, int idx, int[] memo){
//base case
if(idx>=nums.length) return 0;

if(memo[idx]!=0) return memo[idx];

//logic
//no choose
int case1=helper(nums,idx+1,memo);
//choose
int case2=nums[idx]+helper(nums,idx+2,memo);
memo[idx]=Math.max(case1,case2);

return Math.max(case1,case2);

}
}

//Way:3
//Recurssion-- choose or no choose -- causing time limit exceeded
//So, going with memoization-- remembering the result
//TC: O(n)
class Solution {
public int rob(int[] nums) {
int[] memo = new int[nums.length];
//TO avoid time limit exceeded for the input with all elements as 0
for(int i=0;i<nums.length;i++){
memo[i]=Integer.MIN_VALUE;
}
return helper(nums,0,memo);

}

private int helper (int[] nums, int idx, int[] memo){
//base case
if(idx>=nums.length) return 0;

if(memo[idx]!=Integer.MIN_VALUE) return memo[idx];

//logic
//no choose
int case1=helper(nums,idx+1,memo);
//choose
int case2=nums[idx]+helper(nums,idx+2,memo);
memo[idx]=Math.max(case1,case2);

return Math.max(case1,case2);

}
}

//Way4:
//DP: tabulation
//We are choosing alternate houses. We can have one dp array which holds maximum value at that particular index
//At every index we have two options. To choose the house or not to choose the house
//Choose the house: sum of curr house and max value at (curr house -2)
//Not to choose the house: Max value at prev house i.e., currhouse-1
//Choose max value from above steps
//TC: O(n); SC: O(n)
class Solution {
public int rob(int[] nums) {
int n=nums.length;
if(n==1) return nums[0];
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(dp[i-1],nums[i]+dp[i-2]);
}

return dp[n-1];

}
}
137 changes: 137 additions & 0 deletions Leetcode_322.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
//Way1
//Exhaustive - choose and no choose
//TC: 2^(m+n); SC: stack space
class Solution {
public int coinChange(int[] coins, int amount) {
int ans= helper(coins,amount,0);
if(ans==Integer.MAX_VALUE-10) return -1;
return ans;

}

private int helper(int[] coins, int amount, int idx){
//base case
if(idx==coins.length || amount<0) return Integer.MAX_VALUE-10;

if(amount==0) return 0;

//logic:
//no choose
int case1 = helper(coins,amount,idx+1);

//choose
int case2=1+helper(coins,amount-coins[idx],idx);

return Math.min(case1,case2);


}
}


//Way2:
//When we have opted for choose and no choose case, we got many repeated sub problems. So, we can choose dp to solve it.
//In dp, going with bottom up i.e., tabulation method.
//TC: O(m*n); SC: O(m*n)
class Solution {
public int coinChange(int[] coins, int amount) {
int m=coins.length;
int n=amount;
int[][] dp=new int[m+1][n+1];

//Number of coins required to make amount 0 with coins 0
dp[0][0]=0;
//Number of coins required to make amount n with coins 0
for(int i=1;i<=n;i++){
dp[0][i]=Integer.MAX_VALUE-10;
}

for(int i=1;i<=m;i++){
for(int j=0;j<=n;j++){
//Using previous rows to get current row.
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]]);
}

}

}

if(dp[m][n]==Integer.MAX_VALUE-10) return -1;
return dp[m][n];
}
}

//Way3:
//When we have opted for choose and no choose case, we got many repeated sub problems. So, we can choose dp to solve it.
//In dp, going with bottom up i.e., tabulation method.
//TC: O(m*n); SC: O(n)
class Solution {
public int coinChange(int[] coins, int amount) {
int m=coins.length;
int n=amount;
int[] dp=new int[n+1];

//Number of coins required to make amount 0 with coins 0
dp[0]=0;
//Number of coins required to make amount n with coins 0
for(int i=1;i<=n;i++){
dp[i]=Integer.MAX_VALUE-10;
}


for(int i=1;i<=m;i++){
for(int j=0;j<=n;j++){
//Using previous rows to get current row.
if(j<coins[i-1]){
dp[j]=dp[j];
}else{
dp[j]=Math.min(dp[j],1+dp[j-coins[i-1]]);
}
}
}

if(dp[n]==Integer.MAX_VALUE-10) return -1;
return dp[n];
}
}

//Way4:
//Exhaustive - choose and no choose -- causing time limit exceeded
//So, opt for exhaustive with memoization. It means remembering the prev results
//TC: O(m*n); SC: O(m*n)
class Solution {
public int coinChange(int[] coins, int amount) {
int m=coins.length;
int n=amount;

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

int ans= helper(coins,amount,0,memo);
if(ans==Integer.MAX_VALUE-10) return -1;
return ans;

}

private int helper(int[] coins, int amount, int idx, int[][] memo){
//base case
if(idx==coins.length || amount<0) return Integer.MAX_VALUE-10;

if(amount==0) return 0;

if(memo[idx][amount]!=0) return memo[idx][amount];

//logic:
//no choose
int case1 = helper(coins,amount,idx+1,memo);

//choose
int case2=1+helper(coins,amount-coins[idx],idx,memo);
memo[idx][amount]=Math.min(case1,case2);
return Math.min(case1,case2);


}
}