-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0213-house-robber-ii.cpp
40 lines (32 loc) · 998 Bytes
/
0213-house-robber-ii.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
Given int array in a circle, return max amount can rob (can't rob adj houses)
Ex. nums = [2,3,2] -> 3, can't rob house 1 & 3 b/c circular adj, so rob 2
Recursion w/ memo -> DP, rob either 2 away + here, or 1 away, try both ranges
Recurrence relation: robFrom[i] = max(robFrom[i-2] + nums[i], robFrom[i-1])
Time: O(n)
Space: O(1)
*/
class Solution {
public:
int rob(vector<int>& nums) {
int n = nums.size();
if (n == 1) {
return nums[0];
}
int range1 = robber(nums, 0, n - 2);
int range2 = robber(nums, 1, n - 1);
return max(range1, range2);
}
private:
int robber(vector<int>& nums, int start, int end) {
int prev = 0;
int curr = 0;
int next = 0;
for (int i = start; i <= end; i++) {
next = max(prev + nums[i], curr);
prev = curr;
curr = next;
}
return curr;
}
};