-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprob213.java
More file actions
30 lines (28 loc) · 764 Bytes
/
prob213.java
File metadata and controls
30 lines (28 loc) · 764 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
class Solution {
public int rob(int[] arr) {
int n=arr.length;
if(n==0)
return 0;
else if(n==1)
return arr[0];
else if(n==2)
return Math.max(arr[0],arr[1]);
return Math.max(frame(arr,0,n-2),frame(arr,1,n-1));
}
public int frame(int[]arr,int s,int e){
int n=arr.length;
int[]dp=new int[n];
for(int i=e;i>=s;i--){
int a=arr[i];
if(i==e || i==e-1 || i==e-2){
if(i==e-2)
dp[i]=arr[i]+arr[i+2];
else
dp[i]=arr[i];
continue;
}
dp[i]=arr[i]+Math.max(dp[i+2],dp[i+3]);
}
return Math.max(dp[s],dp[s+1]);
}
}