Skip to content

Commit 19b9531

Browse files
authored
Add House_robber_II c++
This program solve issue Rohit0301#61 Leetcode Question Link: https://leetcode.com/problems/kth-largest-element-in-an-array/description/
1 parent c249010 commit 19b9531

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Leetcode/House_Robber_II.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
Question link: https://leetcode.com/problems/house-robber-ii/description/
3+
4+
*/
5+
6+
7+
8+
class Solution {
9+
public:
10+
int rob(vector<int>& nums) {
11+
int n = nums.size();
12+
if (n < 2) return n ? nums[0] : 0;
13+
return max(robber(nums, 0, n - 2), robber(nums, 1, n - 1));
14+
}
15+
private:
16+
int robber(vector<int>& nums, int l, int r) {
17+
int pre = 0, cur = 0;
18+
for (int i = l; i <= r; i++) {
19+
int temp = max(pre + nums[i], cur);
20+
pre = cur;
21+
cur = temp;
22+
}
23+
return cur;
24+
}
25+
};

0 commit comments

Comments
 (0)