Skip to content

Commit f4ae312

Browse files
committed
Time: 0 ms (100.00%), Space: 7.7 MB (40.87%) - LeetHub
1 parent 150a9eb commit f4ae312

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

house-robber/house-robber.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
int rob(vector<int>& nums) {
4+
// dp[i] = max amt of money till ith index
5+
6+
7+
int n = nums.size();
8+
if(n == 0)
9+
return 0;
10+
11+
vector<int> dp(n);
12+
dp[0] = nums[0];
13+
14+
for(int i = 1; i < n; i++){
15+
if(i == 1)
16+
dp[i] = max(nums[i], dp[i-1]);
17+
else
18+
dp[i] = max(dp[i-2] + nums[i], dp[i - 1]);
19+
}
20+
21+
return dp[n-1];
22+
}
23+
};

0 commit comments

Comments
 (0)