-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #171 from Abe0770/master
Added Leetcode Solution for Q-213
- Loading branch information
Showing
2 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> </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> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
- `1 <= nums.length <= 100` | ||
- `0 <= nums[i] <= 1000` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |