Skip to content

Commit eafb1b7

Browse files
PDKhanriver20s
authored andcommitted
house-robber solution
1 parent 3430490 commit eafb1b7

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

house-robber/PDKhan.cpp

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

0 commit comments

Comments
 (0)