Skip to content

Commit f202799

Browse files
committed
house-robber sol
1 parent 5ffa46d commit f202799

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

house-robber/oyeong011.cpp

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

0 commit comments

Comments
 (0)