From ed68b0525b509a1abeb69f568356430fba373df7 Mon Sep 17 00:00:00 2001 From: rbhargav0104 Date: Thu, 1 Jan 2026 17:35:10 +0530 Subject: [PATCH] Completed DP-1 --- coinchange.cpp | 0 houserobber.cpp | 21 +++++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 coinchange.cpp create mode 100644 houserobber.cpp 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