Skip to content

Commit 5bc78c8

Browse files
committed
Merge branch 'master' of https://github.com/yanglbme/leetcode
2 parents 39b0afd + 235d454 commit 5bc78c8

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Complete solutions to Leetcode problems, updated daily.
1616
| 007 | [Reverse Integer](https://github.com/yanglbme/leetcode/tree/master/solution/007.Reverse%20Integer) | `Math` |
1717
| 021 | [Merge Two Sorted Lists](https://github.com/yanglbme/leetcode/tree/master/solution/021.Merge%20Two%20Sorted%20Lists) | `Linked List` |
1818
| 083 | [Remove Duplicates from Sorted List](https://github.com/yanglbme/leetcode/tree/master/solution/083.Remove%20Duplicates%20from%20Sorted%20List) | `Linked List` |
19+
| 198 | [House Robber](https://github.com/yanglbme/leetcode/tree/master/solution/198.House%20Robber) | `Dynamic Programming` |
1920
| 203 | [Remove Linked List Elements](https://github.com/yanglbme/leetcode/tree/master/solution/203.Remove%20Linked%20List%20Elements) | `Linked List` |
2021
| 237 | [Delete Node in a Linked List](https://github.com/yanglbme/leetcode/tree/master/solution/237.Delete%20Node%20in%20a%20Linked%20List) | `Linked List` |
2122
| 876 | [Middle of the Linked List](https://github.com/yanglbme/leetcode/tree/master/solution/876.Middle%20of%20the%20Linked%20List) | `Linked List` |

solution/198.House Robber/README.md

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
## 打家劫舍
2+
### 题目描述
3+
4+
你是一个专业的小偷,计划偷窃沿街的房屋。每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警。
5+
6+
给定一个代表每个房屋存放金额的非负整数数组,计算你在不触动警报装置的情况下,能够偷窃到的最高金额。
7+
8+
示例 1:
9+
```
10+
输入: [1,2,3,1]
11+
输出: 4
12+
解释: 偷窃 1 号房屋 (金额 = 1) ,然后偷窃 3 号房屋 (金额 = 3)。偷窃到的最高金额 = 1 + 3 = 4 。
13+
```
14+
15+
示例 2:
16+
```
17+
输入: [2,7,9,3,1]
18+
输出: 12
19+
解释: 偷窃 1 号房屋 (金额 = 2), 偷窃 3 号房屋 (金额 = 9),接着偷窃 5 号房屋 (金额 = 1)。偷窃到的最高金额 = 2 + 9 + 1 = 12 。
20+
```
21+
22+
### 解法
23+
每个房屋都有两种选择,抢 or 不抢。从第 n 个房屋开始往前抢,进行递归。
24+
25+
以上递归方式会超时,因为中间有很多重复的计算,我们可以开一个空间,如数组,来记录中间结果,避免重复计算,这也就是动态规划。
26+
27+
递归版(超时):
28+
29+
```java
30+
class Solution {
31+
private int process(int n, int[] nums) {
32+
return n < 0 ? 0 : Math.max(nums[n] + process(n - 2, nums), process(n - 1, nums));
33+
}
34+
35+
public int rob(int[] nums) {
36+
return (nums == null || nums.length == 0) ? 0 : process(nums.length - 1, nums);
37+
}
38+
}
39+
```
40+
41+
动态规划版(改进):
42+
43+
```java
44+
class Solution {
45+
public int rob(int[] nums) {
46+
if (nums == null || nums.length == 0) {
47+
return 0;
48+
}
49+
50+
int n = nums.length;
51+
if (n == 1) {
52+
return nums[0];
53+
}
54+
55+
int[] result = new int[n];
56+
result[0] = nums[0];
57+
result[1] = Math.max(nums[0], nums[1]);
58+
59+
for (int i = 2; i < n; ++i) {
60+
result[i] = Math.max(nums[i] + result[i - 2], result[i - 1]);
61+
}
62+
63+
return result[n - 1];
64+
65+
}
66+
}
67+
```
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public int rob(int[] nums) {
3+
if (nums == null || nums.length == 0) {
4+
return 0;
5+
}
6+
7+
int n = nums.length;
8+
if (n == 1) {
9+
return nums[0];
10+
}
11+
12+
int[] result = new int[n];
13+
result[0] = nums[0];
14+
result[1] = Math.max(nums[0], nums[1]);
15+
16+
for (int i = 2; i < n; ++i) {
17+
result[i] = Math.max(nums[i] + result[i - 2], result[i - 1]);
18+
}
19+
20+
return result[n - 1];
21+
22+
}
23+
}

0 commit comments

Comments
 (0)