-
Notifications
You must be signed in to change notification settings - Fork 452
Expand file tree
/
Copy pathSolution4.java
More file actions
30 lines (24 loc) · 827 Bytes
/
Solution4.java
File metadata and controls
30 lines (24 loc) · 827 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
/// 198. House Robber
/// https://leetcode.com/problems/house-robber/description/
/// 动态规划, 改变状态定义
/// 时间复杂度: O(n^2)
/// 空间复杂度: O(n)
public class Solution4 {
public int rob(int[] nums) {
int n = nums.length;
if(n == 0)
return 0;
// memo[i] 表示考虑抢劫 nums[0...i] 所能获得的最大收益
int[] memo = new int[nums.length];
memo[0] = nums[0];
for(int i = 1 ; i < n ; i ++)
for (int j = i; j >= 0; j--)
memo[i] = Math.max(memo[i],
nums[j] + (j - 2 >= 0 ? memo[j - 2] : 0));
return memo[n-1];
}
public static void main(String[] args) {
int nums[] = {2, 1};
System.out.println((new Solution4()).rob(nums));
}
}