diff --git a/coin_change.cpp b/coin_change.cpp new file mode 100644 index 00000000..a3e5e836 --- /dev/null +++ b/coin_change.cpp @@ -0,0 +1,59 @@ +class Solution { +public: + int coinChange(vector& coins, int amount) { + //TC: O(m*n) + //SC: O(m*n) + vector> dp(coins.size()+1,vector(amount+1)); + if(coins.size()==0) + return -1; + + for(int i=0;i& coins, int index, int amount, int coinsUsed) + { + if(index==coins.size()|| amount<0) return -1; + if(amount==0) return coinsUsed; + + int case2= recurse(coins, index, amount-coins[index], coinsUsed+1); + int case1=recurse(coins, index+1, amount, coinsUsed); + + + if(case1==-1) return case2; + if(case2==-1) return case1; + return min(case1, case2); + }*/ + + + } +}; + + diff --git a/house_robber.cpp b/house_robber.cpp new file mode 100644 index 00000000..cf4dc0e7 --- /dev/null +++ b/house_robber.cpp @@ -0,0 +1,56 @@ +class Solution { +public: + int rob(vector& nums) { + //Appraoch 1 - go exhaustive- all possible value evaluation + + // TC: 2^n + // SC: O(n) + // return helper(nums, 0, 0); + // } + // int helper(vector& nums, int index, int earned) + // { + // if(index >= nums.size()) + // { + // return earned; + // } + // //not choose + // int case1=helper(nums, index+1, earned); + // //choose + // int case2=helper(nums, index+2, earned+nums[index]); + + // return max(case1, case2); + // } + + //Approach 2: *******2D array***** dp where we store values of robbings and values when not robbed. if we choose to not rob the current house then the value should come from earlier robbings!! if chose to rob the current house then current house robbing value must be added to the earlier non-robbed value because only alternate hosues can be robbed. in the end we get maximum between both the approaches + + // TC: O(n) + // SC: O(n) + + // int n=nums.size(); + // vector> dp(n,vector(2)); + // dp[0][0]=0; + // dp[0][1]=nums[0]; + // for(int i=1;i