forked from algorithm009-class01/algorithm009-class01
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRobber2.java
More file actions
29 lines (25 loc) · 744 Bytes
/
Robber2.java
File metadata and controls
29 lines (25 loc) · 744 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
package dynamic;
import java.util.Arrays;
public class Robber2 {
public int rob(int[] nums){
if (nums.length == 0) return 0;
if (nums.length == 1) return nums[0];
return Math.max(myRob(Arrays.copyOfRange(nums,0,nums.length-1)),
myRob(Arrays.copyOfRange(nums,1,nums.length)));
}
private int myRob(int[] nums) {
int pre = 0,cur = 0,tmp;
for (int num : nums){
tmp = cur;
cur = Math.max(pre+num,cur);
pre = tmp;
}
return cur;
}
public static void main(String[] args) {
int[] nums = {1,2,3,1};
Robber2 robber2 = new Robber2();
int r = robber2.rob(nums);
System.out.println(r);
}
}