Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4c3553b

Browse files
zhangzz2015gitbook-bot
authored andcommitted
GitBook: [greyireland#100] No subject
1 parent 2dd1588 commit 4c3553b

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

‎basic_algorithm/dp.md‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,31 @@ func climbStairs(n int) int {
310310
}
311311
```
312312

313+
### [House Robber](https://leetcode.com/problems/house-robber/)
314+
315+
```cpp
316+
// Some code
317+
class Solution {
318+
public:
319+
int rob(vector<int>& nums) {
320+
321+
// dp[i] = max(nums[i] + dp[i-2], dp[i-1]);
322+
int prevPrev =0;
323+
int prev = 0;
324+
for(int i=0; i< nums.size(); i++)
325+
{
326+
int current = max(nums[i] + prevPrev, prev);
327+
328+
prevPrev = prev;
329+
prev = current;
330+
}
331+
332+
return prev;
333+
334+
}
335+
};
336+
```
337+
313338
### [jump-game](https://leetcode-cn.com/problems/jump-game/)
314339
315340
> 给定一个非负整数数组,你最初位于数组的第一个位置。 数组中的每个元素代表你在该位置可以跳跃的最大长度。 判断你是否能够到达最后一个位置。

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /