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
45 changes: 45 additions & 0 deletions coin_change.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// class Solution {
// public int coinChange(int[] coins, int amount) {
// //Brute force - Exhaustive
// int re = helper(coins,amount,0);
// if(re<Integer.MAX_VALUE-10) return re;
// return -1;
// }
// private int helper(int[] coins, int amount, int idx){
// //base
// if(idx==coins.length || amount<0) return Integer.MAX_VALUE-10;
// if(amount==0) return 0;
// //logic
// //Not choose
// int case1= helper(coins,amount,idx+1);
// //choose
// int case2=1+helper(coins,amount-coins[idx],idx);
// return Math.min(case1,case2);
// }
// }


// Time Complexity : O(m*n) where m is the number of coins and n is the amount
// Space Complexity : O(n) where n is the amount
// Did this code successfully run on Leetcode : yes
class Solution {
public int coinChange(int[] coins, int amount) {
int m= coins.length;
int n= amount;
int[] dp = new int[n+1];
dp[0]=0;
for(int i=1;i<=n;i++){
dp[i]= Integer.MAX_VALUE -10;
}
for(int i=1; i<=m;i++){
for(int j=coins[i-1];j<=n;j++){
dp[j] = Math.min(dp[j],1+dp[j-coins[i-1]]);
}
}
if(dp[n]<Integer.MAX_VALUE -10) return dp[n];
return -1;
}
}

// Bottom up apprach - Matrix as the decision making variable are the coins and the amount.
// then i reduced it to tabluation and then to space optimization.
74 changes: 74 additions & 0 deletions house_robber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Exhaustive method
// class Solution {
// public int rob(int[] nums) {
// return helper(nums,0);
// }
// private int helper(int[] nums, int idx){
// //base
// if(idx>=nums.length) return 0;
// //logic
// //dont choose
// int case1 = helper(nums,idx+1);
// //choose
// int case2 = nums[idx]+helper(nums,idx+2);
// return Math.max(case1, case2);
// }
// }

// Top Dowm approach
// import java.util.Arrays;
// class Solution {
// int[] memo;
// public int rob(int[] nums) {
// this.memo= new int[nums.length];
// Arrays.fill(memo,-1);
// return helper(nums,0);
// }
// private int helper(int[] nums, int idx){
// //base
// if(idx>=nums.length) return 0;
// if(memo[idx]!=-1) return memo[idx];
// //logic
// //dont choose
// int case1 = helper(nums,idx+1);
// //choose
// int case2 = nums[idx]+helper(nums,idx+2);
// memo[idx]= Math.max(case1, case2);
// return memo[idx];
// }
// }


//Bottom up approach
// 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<nums.length;i++){
// dp[i] = Math.max(dp[i-1],nums[i]+dp[i-2]);
// }
// return dp[n-1];
// }
// }

//Bottom up approach - memory optimised
// Time Complexity : O(n) where n is the length of the input array
// Space Complexity : O(1) as we are using constant space for the variables
class Solution {
public int rob(int[] nums) {
int n = nums.length;
if(n == 1) return nums[0];
//int[] dp = new int[n];
int prev = nums[0];
int curr = Math.max(nums[0], nums[1]);
for(int i=2; i<nums.length;i++){
int temp = curr;
curr = Math.max(curr,nums[i]+prev);
prev = temp;
}
return curr;
}
}