-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSticklerThief.java
54 lines (49 loc) · 1.44 KB
/
SticklerThief.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
52
53
54
/*https://practice.geeksforgeeks.org/problems/stickler-theif-1587115621/1/*/
/*Prateekshya*/
/*O(n^2) solution*/
class Solution
{
public int FindMaxSum(int arr[], int n)
{
if (n == 1) return arr[0];
int[] table = new int[n];
int[] maxTillNow = new int[n];
int result = 0;
maxTillNow[0] = table[0] = arr[0];
table[1] = arr[1];
maxTillNow[1] = result = Math.max(table[0],table[1]);
if (n == 2) return result;
for (int i = 2; i < n; ++i)
{
for (int j = i-2; j >= 0; --j) //i-2 because i-1 will be adjacent
{
if (arr[i]+table[j] > table[i])
table[i] = arr[i]+table[j];
}
result = Math.max(result,table[i]);
}
return result;
}
}
/*O(n) solution*/
class Solution
{
public int FindMaxSum(int arr[], int n)
{
if (n == 1) return arr[0];
int[] table = new int[n];
int[] maxTillNow = new int[n];
int result = 0;
maxTillNow[0] = table[0] = arr[0];
table[1] = arr[1];
maxTillNow[1] = result = Math.max(table[0],table[1]);
if (n == 2) return result;
for (int i = 2; i < n; ++i)
{
table[i] = arr[i]+maxTillNow[i-2];
maxTillNow[i] = Math.max(maxTillNow[i-1],table[i]); //to reduce time complexity
result = Math.max(result,table[i]);
}
return result;
}
}