forked from liuyubobobo/Play-with-Algorithm-Interview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution6.java
33 lines (26 loc) · 966 Bytes
/
Solution6.java
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
33
import java.util.Arrays;
/// 198. House Robber
/// https://leetcode.com/problems/house-robber/description/
/// 动态规划, 优化状态转移
/// 时间复杂度: O(n)
/// 空间复杂度: O(n)
public class Solution6 {
public int rob(int[] nums) {
int n = nums.length;
if(n == 0)
return 0;
// memo[i] 表示考虑抢劫 nums[i...n) 所能获得的最大收益
int[] memo = new int[nums.length];
memo[n - 1] = nums[n - 1];
for(int i = n - 2 ; i >= 0 ; i --)
// 或者当前房子放弃, 从下一个房子开始考虑
// 或者抢劫当前的房子, 从i+2以后的房子开始考虑
memo[i] = Math.max(memo[i + 1],
nums[i] + (i + 2 < n ? memo[i + 2] : 0));
return memo[0];
}
public static void main(String[] args) {
int nums[] = {2, 1};
System.out.println((new Solution6()).rob(nums));
}
}