-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHouseRobber.java
More file actions
43 lines (37 loc) · 1.21 KB
/
HouseRobber.java
File metadata and controls
43 lines (37 loc) · 1.21 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
import java.util.HashMap;
import java.util.Map;
public class HouseRobber {
private Map<Integer, Integer> cache = new HashMap<Integer, Integer>();
public int robot(int index, int[] nums) {
if (index >= nums.length) {
return 0;
}
if (cache.containsKey(index)) {
return cache.get(index);
}
int a = nums[index] + robot(index + 2, nums);
int b = 0 + robot(index + 1, nums);
int c = Math.max(a, b);
cache.put(index, c);
return c;
}
// 1. 递归方式
public int rob(int[] nums) {
cache.clear();
if (nums.length == 0) return 0;
return robot(0, nums);
}
// 2. 递推方式
public int rob(int[] nums){
if (nums.length == 0) return 0;
if (nums.length == 1) return nums[0];
int[] result = new int[nums.length];
result[0] = nums[0];
result[1] = Math.max(nums[0], nums[1]);
for(int index = 2; index < nums.length; index++){
result[index] = Math
.max(nums[index] + result[index - 2], result[index - 1]);
}
return result[nums.length - 1];
}
}