Skip to content

Commit 7009755

Browse files
committed
add 198
1 parent 5e88955 commit 7009755

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
| 70 | [Climbing Stairs][070] |
7777
| 121 | [Best Time to Buy and Sell Stock][121] |
7878
| 53 | [Maximum Subarray][053] |
79+
| 198 | [House Robber][198] |
7980

8081

8182
[leetcode]: https://leetcode.com/problemset/all/
@@ -118,3 +119,4 @@
118119
[070]: https://github.com/andavid/leetcode-java/blob/master/note/070/README.md
119120
[121]: https://github.com/andavid/leetcode-java/blob/master/note/121/README.md
120121
[053]: https://github.com/andavid/leetcode-java/blob/master/note/053/README.md
122+
[198]: https://github.com/andavid/leetcode-java/blob/master/note/198/README.md

note/198/README.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# [House Robber][title]
2+
3+
## Description
4+
5+
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
6+
7+
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.
8+
9+
## 思路
10+
11+
假设 dp[i] 表示抢劫前 i 个房子的最大收益,由于不能连着抢两个房子,所以 dp[i] = Max(dp[i-2]+ nums[i], dp[i-1])
12+
13+
## [完整代码][src]
14+
15+
```java
16+
class Solution {
17+
public int rob(int[] nums) {
18+
if (nums == null || nums.length == 0) return 0;
19+
if (nums.length == 1) return nums[0];
20+
21+
int past = nums[0];
22+
int prev = Math.max(nums[0], nums[1]);
23+
int max = prev;
24+
25+
for (int i = 2; i < nums.length; i++) {
26+
int sum = past + nums[i];
27+
if (prev > sum) sum = prev;
28+
past = prev;
29+
prev = sum;
30+
if (sum > max) max = sum;
31+
}
32+
33+
return max;
34+
}
35+
}
36+
```
37+
38+
[title]: https://leetcode.com/problems/house-robber
39+
[src]: https://github.com/andavid/leetcode-java/blob/master/src/com/andavid/leetcode/_198/Solution.java
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public int rob(int[] nums) {
3+
if (nums == null || nums.length == 0) return 0;
4+
if (nums.length == 1) return nums[0];
5+
6+
int past = nums[0];
7+
int prev = Math.max(nums[0], nums[1]);
8+
int max = prev;
9+
10+
for (int i = 2; i < nums.length; i++) {
11+
int sum = past + nums[i];
12+
if (prev > sum) sum = prev;
13+
past = prev;
14+
prev = sum;
15+
if (sum > max) max = sum;
16+
}
17+
18+
return max;
19+
}
20+
21+
public static void main(String[] args) {
22+
Solution solution = new Solution();
23+
int[] nums = {2, 1, 1, 2};
24+
System.out.println(solution.rob(nums));
25+
}
26+
}

0 commit comments

Comments
 (0)