-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProblem-3.java
51 lines (37 loc) · 935 Bytes
/
Problem-3.java
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
/**
* @author AkashGoyal
*/
/**
--------------------- Problem----------->> Stickler Thief (maximum sum such that no 2 elements are adjacent)
Problem Link :- https://practice.geeksforgeeks.org/problems/stickler-theif-1587115621/1
*/
class Solution
{
//Function to find the maximum money the thief can get.
int dp_arr[]=new int[10000];
public int FindMaxSum(int arr[], int n)
{
// Your code here
for(int i=0;i<10000;i++)
{
dp_arr[i]=-1;
}
return Math.max(dp_max(arr,n-1),dp_max(arr,n-2));
}
int dp_max(int arr[],int n)
{
if(n<0)
{
return 0;
}
if(n==0)
{
return arr[0];
}
if(dp_arr[n]!=-1)
{
return dp_arr[n];
}
return dp_arr[n]=arr[n]+Math.max(dp_max(arr,n-3),dp_max(arr,n-2));
}
}