|
| 1 | +// Approach 1: Recursion with Index Tracking |
| 2 | +// In this approach, we use a helper function that tries to rob each house starting from index `i`. |
| 3 | +// We have two choices: either rob the current house (skip the next house), or skip the current house |
| 4 | +// and consider the next one. The solution is based on comparing both options at each step recursively. |
| 5 | +class Solution { |
| 6 | +public: |
| 7 | + // Function to solve the problem recursively by tracking the current index `i` and total number of houses `n` |
| 8 | + int solve(vector<int>& money, int i, int n) { |
| 9 | + // Base cases: if we exceed the house index range, return 0 (no money left to rob) |
| 10 | + if (i > n) return 0; |
| 11 | + |
| 12 | + // If we reach the last house, return the money at the current index |
| 13 | + if (i == n) return money[i]; |
| 14 | + |
| 15 | + // Option 1: Rob this house and skip the next one (i+2) |
| 16 | + int include = solve(money, i + 2, n) + money[i]; |
| 17 | + |
| 18 | + // Option 2: Skip this house and move to the next one (i+1) |
| 19 | + int exclude = solve(money, i + 1, n) + 0; |
| 20 | + |
| 21 | + // Return the maximum of both choices (rob this house or skip it) |
| 22 | + return max(include, exclude); |
| 23 | + } |
| 24 | + |
| 25 | + // Main function to initiate the recursion and find the maximum money that can be robbed |
| 26 | + int rob(vector<int>& nums) { |
| 27 | + int n = nums.size(); |
| 28 | + |
| 29 | + // Call the helper function starting from the first house (index 0) |
| 30 | + int maxMoney = solve(nums, 0, n - 1); |
| 31 | + |
| 32 | + // Return the maximum money that can be robbed |
| 33 | + return maxMoney; |
| 34 | + } |
| 35 | +}; |
| 36 | + |
| 37 | + |
| 38 | +// Approach 2: Recursion with Index Backtracking |
| 39 | +// In this approach, we start from the last house and recursively decide whether to rob this house or not. |
| 40 | +// We backtrack to the previous house and calculate the total money robbed based on whether we include or exclude the current house. |
| 41 | +class Solution { |
| 42 | +public: |
| 43 | + // Function to solve the problem recursively by considering houses starting from the last index `n` |
| 44 | + int solve(vector<int>& money, int n) { |
| 45 | + // Base cases: if index `n` is out of bounds, return 0 |
| 46 | + if (n < 0) return 0; |
| 47 | + |
| 48 | + // If we are at the first house, return the amount of money in the first house |
| 49 | + if (n == 0) return money[0]; |
| 50 | + |
| 51 | + // Option 1: Rob this house and skip the next one (n-2) |
| 52 | + int include = solve(money, n - 2) + money[n]; |
| 53 | + |
| 54 | + // Option 2: Skip this house and move to the previous one (n-1) |
| 55 | + int exclude = solve(money, n - 1) + 0; |
| 56 | + |
| 57 | + // Return the maximum of both choices (rob this house or skip it) |
| 58 | + return max(include, exclude); |
| 59 | + } |
| 60 | + |
| 61 | + // Main function to initiate the recursion and find the maximum money that can be robbed |
| 62 | + int rob(vector<int>& nums) { |
| 63 | + int n = nums.size(); |
| 64 | + |
| 65 | + // Call the helper function starting from the last house (index n-1) |
| 66 | + int maxMoney = solve(nums, n - 1); |
| 67 | + |
| 68 | + // Return the maximum money that can be robbed |
| 69 | + return maxMoney; |
| 70 | + } |
| 71 | +}; |
| 72 | + |
| 73 | + |
0 commit comments