Skip to content

Commit e97ed52

Browse files
committed
House Robber II solution
1 parent eac1612 commit e97ed52

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

house-robber-ii/PDKhan.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
int robsearch(vector<int>& nums, int start, int end) {
4+
int prev = 0;
5+
int prevprev = 0;
6+
7+
for(int i = start; i <= end; i++){
8+
int curr = max(prev, nums[i] + prevprev);
9+
10+
prevprev = prev;
11+
prev = curr;
12+
}
13+
14+
return prev;
15+
}
16+
17+
int rob(vector<int>& nums) {
18+
int len = nums.size();
19+
20+
if(len == 0)
21+
return 0;
22+
if(len == 1)
23+
return nums[0];
24+
25+
return max(robsearch(nums, 0, len - 2), robsearch(nums, 1, len - 1));
26+
}
27+
};

0 commit comments

Comments
 (0)