Skip to content

Commit 78bc4e1

Browse files
Merge pull request #171 from Abe0770/master
Added Leetcode Solution for Q-213
2 parents 0a0da92 + 15273c1 commit 78bc4e1

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

Cpp/0213-house-robber-II/problem.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<h1><a href = "https://leetcode.com/problems/house-robber-ii/description/">213. House Robber II</h1>
2+
3+
## Problem Statement
4+
5+
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.
6+
7+
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.
8+
9+
<p>&nbsp;</p>
10+
<p><strong class="example">Example 1:</strong></p>
11+
<pre>
12+
<strong>Input: </strong> nums = [2,3,2]
13+
<strong>Output:</strong> 3
14+
<strong>Explanation:</strong> You cannot rob house 1 (money = 2) and then rob house 3 (money = 2),
15+
because they are adjacent houses.
16+
</pre>
17+
18+
<p><strong class="example">Example 2:</strong></p>
19+
<pre>
20+
<strong>Input:</strong> nums = [1,2,3,1]
21+
<strong>Output:</strong> 4
22+
<strong>Explanation:</strong> Rob house 1 (money = 1) and then rob house 3 (money = 3).
23+
Total amount you can rob = 1 + 3 = 4.
24+
</pre>
25+
26+
<p><strong class="example">Example 3:</strong></p>
27+
<pre>
28+
<strong>Input:</strong> nums = [1,2,3]
29+
<strong>Output:</strong> 3
30+
</pre>
31+
<p>&nbsp;</p>
32+
<p><strong>Constraints:</strong></p>
33+
34+
- `1 <= nums.length <= 100`
35+
- `0 <= nums[i] <= 1000`

Cpp/0213-house-robber-II/solution.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public:
3+
int rob(vector<int>& nums) {
4+
int n = nums.size();
5+
6+
// Handle edge cases
7+
if (n == 0) return 0; // No houses
8+
if (n == 1) return nums[0]; // Only one house
9+
10+
// Helper function to calculate max loot for a linear array
11+
auto robLinear = [](const vector<int>& nums, int start, int end) {
12+
int prev1 = 0, prev2 = 0; // Initialize previous two max values
13+
for (int i = start; i < end; ++i) {
14+
int pick = nums[i] + prev1; // Value if we pick current house
15+
int notPick = prev2; // Value if we do not pick current house
16+
int curr = max(pick, notPick); // Max of both options
17+
prev1 = prev2; // Update previous for the next iteration
18+
prev2 = curr;
19+
}
20+
return prev2; // Return the maximum value obtained
21+
};
22+
23+
// Rob houses excluding the last house and then excluding the first house
24+
int maxLoot1 = robLinear(nums, 0, n - 1); // From house 0 to house n-2
25+
int maxLoot2 = robLinear(nums, 1, n); // From house 1 to house n-1
26+
27+
// Return the maximum of both scenarios
28+
return max(maxLoot1, maxLoot2);
29+
}
30+
};

0 commit comments

Comments
 (0)