forked from DengWangBao/Leetcode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHouseRobber.java
More file actions
32 lines (28 loc) · 823 Bytes
/
HouseRobber.java
File metadata and controls
32 lines (28 loc) · 823 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class HouseRobber {
/**
* 这里的dp表示统计到当前房子的最大收益,但是不确定要不要包含当前房子
*/
public int rob(int[] nums) {
int n = nums.length;
if (n == 0) {
return 0;
}
// dp意思是统计到当前房子为止的最大收益
int[] dp = new int[n];
// 这里要注意越界的问题
for (int i = 0; i < n; i++) {
dp[i] = Math.max(nums[i] + (i > 1 ? dp[i - 2] : 0), i > 0 ? dp[i - 1] : 0);
}
return dp[n - 1];
}
// 这个简洁
public int rob2(int[] nums) {
int prev = 0, cur = 0;
for (int n : nums) {
int temp = cur;
cur = Math.max(n + prev, cur);
prev = temp;
}
return cur;
}
}