Skip to content

Commit

Permalink
Add House_robber_II c++
Browse files Browse the repository at this point in the history
  • Loading branch information
atinder11 authored Oct 3, 2022
1 parent c249010 commit 19b9531
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Leetcode/House_Robber_II.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
Question link: https://leetcode.com/problems/house-robber-ii/description/
*/



class Solution {
public:
int rob(vector<int>& nums) {
int n = nums.size();
if (n < 2) return n ? nums[0] : 0;
return max(robber(nums, 0, n - 2), robber(nums, 1, n - 1));
}
private:
int robber(vector<int>& nums, int l, int r) {
int pre = 0, cur = 0;
for (int i = l; i <= r; i++) {
int temp = max(pre + nums[i], cur);
pre = cur;
cur = temp;
}
return cur;
}
};

0 comments on commit 19b9531

Please sign in to comment.