Skip to content

Commit eb61eb6

Browse files
committed
House Robber solution
1 parent 81d7df4 commit eb61eb6

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

house-robber/ys-han00.cpp

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

0 commit comments

Comments
 (0)