Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fb9d05f

Browse files
committed
Add Q213
1 parent f6e14da commit fb9d05f

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

213_HouseRobber2/HouseRobber2.cc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
//copy from HouseRobber
4+
int rob1(vector<int>& nums, int begin, int end) {
5+
int n = 0, y = 0;
6+
for (int i = begin;i < end;++i) {
7+
int num = nums[i];
8+
int t = n;
9+
n = max(n, y);
10+
y = num + t;
11+
}
12+
return max(n,y);
13+
}
14+
15+
int rob(vector<int>& nums) {
16+
int size = nums.size();
17+
if (size == 0) return 0;
18+
if (size == 1) return nums[0];
19+
return max(rob1(nums, 0, size-1), rob1(nums, 1, size));
20+
}
21+
};

0 commit comments

Comments
 (0)