Skip to content

Commit

Permalink
198 house robber
Browse files Browse the repository at this point in the history
  • Loading branch information
cedard234 committed Oct 24, 2023
1 parent 5ef2854 commit 8b1ae51
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions cpp/198-House-Robber.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public:
int rob(vector<int>& nums) {
int rob(vector<int>& nums) {
if (nums.empty()) return 0;
int n = nums.size();
if (n == 1) return nums[0];
int pre2 = 0, pre1 = 0, cur;
for (int i = 0; i < n; ++i) {
cur = max(pre2 + nums[i], pre1);
pre2 = pre1;
pre1 = cur;
}
return cur;
}
}
};

0 comments on commit 8b1ae51

Please sign in to comment.