-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathDP - 2:Loot Houses
More file actions
29 lines (24 loc) · 851 Bytes
/
DP - 2:Loot Houses
File metadata and controls
29 lines (24 loc) · 851 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
public class Solution {
//recursion
// public static int getMaxMoney(int arr[], int n){
// return getMaxMoney(arr, n,0);
// }
// public static int getMaxMoney(int arr[], int n,int index){
// if( arr.length==0 || index>=arr.length)
// return 0;
// int op1=arr[index]+getMaxMoney(arr,n,index+2);
// int op2=getMaxMoney(arr,n,index+1);
// return Math.max(op1,op2);
//DP
public static int getMaxMoney(int arr[], int n){
if(arr.length==0)
return 0;
int storage[]=new int[arr.length];
storage[0]=arr[0];
storage[1]=Math.max(arr[1],storage[0]);
for(int i=2;i<storage.length;i++){
storage[i]=Math.max(arr[i]+storage[i-2],storage[i-1]);
}
return storage[storage.length-1];
}
}