-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution198.java
More file actions
67 lines (60 loc) · 1.5 KB
/
Copy pathSolution198.java
File metadata and controls
67 lines (60 loc) · 1.5 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* @Title: Solution198.java——
* @Package EasyCode_02
* @Description: TODO
* @author msdumin@gmail.com
* @date 2019年4月16日 上午11:25:35
* @version V1.0
*/
package EasyCode_02;
import java.util.Arrays;
/**
* @ClassName: Solution198——打家劫舍
* @Description: TODO
* @author msdumin@gmail.com
* @date 2019年4月16日 上午11:25:35
*/
public class Solution198 {
//记忆化搜索方法
public int[] memo;
private int tryRob(int[] nums , int index){
//递归结束条件
if(index >= nums.length)
return 0;
if(memo[index] != -1)
return memo[index];
//递归函数
int res = 0;
for (int i = index; i < nums.length; i++) {
res = Math.max(res , nums[i] + tryRob(nums, i + 2));
}
memo[index] = res;
return res;
}
public int rob(int[] nums) {
memo = new int[nums.length];
Arrays.fill(memo, -1);
int res = tryRob(nums, 0);
return res;
}
//动态规划解法
public int rob1(int[] nums) {
int[] record;
int n = nums.length;
if(nums.length == 0)
return 0;
record = new int[nums.length];
Arrays.fill(record, -1);
//record[i] 表示考虑偷取[i...n-1]范围内的房子
record[n-1] = nums[n-1];
for(int i = n-2 ; i >= 0 ; i --)
for(int j = i ; j < n ; j ++){
record[i] = Math.max(record[i], nums[j] + (j+2 < n ? record[j + 2] : 0));
}
return record[0];
}
public static void main(String[] args) {
int[] nums = {1,2,3,1};
System.out.println(new Solution198().rob1(nums));
}
}