Skip to content

Commit

Permalink
Merge pull request #171 from Abe0770/master
Browse files Browse the repository at this point in the history
Added Leetcode Solution for Q-213
  • Loading branch information
JenilGajjar20 authored Oct 10, 2024
2 parents 0a0da92 + 15273c1 commit 78bc4e1
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
35 changes: 35 additions & 0 deletions Cpp/0213-house-robber-II/problem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<h1><a href = "https://leetcode.com/problems/house-robber-ii/description/">213. House Robber II</h1>

## Problem Statement

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have a security system connected, and it will automatically contact the police if two adjacent houses are broken into on the same night.

Given an integer array `nums` representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong> nums = [2,3,2]
<strong>Output:</strong> 3
<strong>Explanation:</strong> You cannot rob house 1 (money = 2) and then rob house 3 (money = 2),
because they are adjacent houses.
</pre>

<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,1]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 = 4.
</pre>

<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3]
<strong>Output:</strong> 3
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

- `1 <= nums.length <= 100`
- `0 <= nums[i] <= 1000`
30 changes: 30 additions & 0 deletions Cpp/0213-house-robber-II/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Solution {
public:
int rob(vector<int>& nums) {
int n = nums.size();

// Handle edge cases
if (n == 0) return 0; // No houses
if (n == 1) return nums[0]; // Only one house

// Helper function to calculate max loot for a linear array
auto robLinear = [](const vector<int>& nums, int start, int end) {
int prev1 = 0, prev2 = 0; // Initialize previous two max values
for (int i = start; i < end; ++i) {
int pick = nums[i] + prev1; // Value if we pick current house
int notPick = prev2; // Value if we do not pick current house
int curr = max(pick, notPick); // Max of both options
prev1 = prev2; // Update previous for the next iteration
prev2 = curr;
}
return prev2; // Return the maximum value obtained
};

// Rob houses excluding the last house and then excluding the first house
int maxLoot1 = robLinear(nums, 0, n - 1); // From house 0 to house n-2
int maxLoot2 = robLinear(nums, 1, n); // From house 1 to house n-1

// Return the maximum of both scenarios
return max(maxLoot1, maxLoot2);
}
};

0 comments on commit 78bc4e1

Please sign in to comment.