Skip to content

Commit 588f3ea

Browse files
authored
Merge pull request #11 from BodomMoon/leetcode198
Update house-robber.cpp
2 parents c7374e1 + ba1aadb commit 588f3ea

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

C++/house-robber.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
int rob(vector<int>& nums) {
7+
int last = 0, result = 0;
8+
for (const auto& i : nums) {
9+
auto tmp = result;
10+
result = max(last + i, result);
11+
last = tmp;
12+
}
13+
return result;
14+
}
15+
};

0 commit comments

Comments
 (0)