diff --git a/coinchange.cpp b/coinchange.cpp new file mode 100644 index 00000000..e69de29b diff --git a/houserobber.cpp b/houserobber.cpp new file mode 100644 index 00000000..066483ab --- /dev/null +++ b/houserobber.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + int rob(vector& nums) { + if(nums.size() < 2) return nums[0];//single element in the array,so return the element itself + + vector maxloot(nums.size());//declare a new array to track the max loot + + maxloot[0] = nums[0]; + maxloot[1] = max(nums[0],nums[1]);//max of 1st and 2nd since you cant loot both + + for(int i = 2;i < nums.size();i++){ + maxloot[i] = max(maxloot[i-1],maxloot[i-2] + nums[i]); + //case 1 : looting current house -> check current 'i'house value in nums and 'i-2' in maxloot + //case 2 : skipping the current house -> check 'i-1' value in maxloot + //final step : compare and update in maxloot + } + + return maxloot[nums.size() - 1 ]; + //return the last element of maxloot + } +}; \ No newline at end of file