Skip to content

Commit a39835a

Browse files
author
zouwx2cs
committed
198.House Robber cpp version (0ms)
1 parent 16ae87b commit a39835a

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
int rob(vector<int>& nums) {
4+
if (nums.size() == 0)
5+
return 0 ;
6+
if (nums.size() == 1)
7+
return nums[0] ;
8+
vector<int> dp(nums.size(), -1) ;
9+
dp[0] = nums[0] ;
10+
dp[1] = max(nums[0], nums[1]) ;
11+
for (int i = 2; i < nums.size(); ++i)
12+
{
13+
dp[i] = max(
14+
dp[i-2] + nums[i],
15+
dp[i-1]
16+
) ;
17+
}
18+
return dp[dp.size()-1] ;
19+
}
20+
};

0 commit comments

Comments
 (0)