-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathPartitionArrayForMaximumSum.java
More file actions
82 lines (68 loc) · 2.44 KB
/
PartitionArrayForMaximumSum.java
File metadata and controls
82 lines (68 loc) · 2.44 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*https://leetcode.com/problems/partition-array-for-maximum-sum/*/
/*
recur for the rest of the array by taking the maximum of the current subarray
e.g.
1,4,1,5,7,3,6,1,9,9,3
k = 4
maximum of ( recursion on 1,4,1,5,7,3,6,1,9,9 + maximum of (3),
recursion on 1,4,1,5,7,3,6,1,9 + maximum of (9,3),
recursion on 1,4,1,5,7,3,6,1 + maximum of (9,9,3),
recursion on 1,4,1,5,7,3,6 + maximum of (1,9,9,3), )
base case will be when the size of the remaining subarray on the left is less than k
*/
class Solution {
Integer[] dp;
public int maxSumAfterPartitioning(int[] arr, int k) {
dp = new Integer[arr.length+1];
return recur(arr, k, arr.length-1);
}
public int recur(int[] a, int k, int end)
{
if (dp[end] != null) return dp[end]; //if already calculated, return it
if (end < k) //when subarray size goes below k
{
//find the maximum of the subarray
int max = a[0];
for (int i = 1; i <= end; ++i) if (a[i] > max) max = a[i];
dp[end] = max*(end+1); //store in table by multiplying the number of elements in the subarray
return dp[end]; //return the result
}
int result = 0, max = 0, rest, i, j;
for (i = end-1; i >= end-k; --i) //move from the end of the current subarray towards left
{
rest = recur(a,k,i); //call recursion for the rest of the subarray
//find maximum of the current subarray
max = a[end];
for (j = end; j > i; --j)
max = Math.max(max,a[j]);
result = Math.max(result,(max*(end-i))+rest); //result will be the sum of maximum of current subarray and the result of the recursion call
}
//store the result and return
dp[end] = result;
return dp[end];
}
}
//faster on leetcode, same time complexity
class Solution {
public int maxSumAfterPartitioning(int[] arr, int k) {
int[] dp=new int[arr.length+1];
dp[0]=0;
dp[1]=arr[0];
for(int i=1;i<arr.length;i++)
dp[i+1]=getMax(arr,i,k,dp);
return dp[dp.length-1];
}
public int getMax(int[] arr,int i,int k,int[] dp){
int max=0;
int c=1;
int v=0;
while(i>=0 && k>0){
max=Math.max(arr[i],max);
v=Math.max(v,dp[i]+c*max);
i--;
k--;
c++;
}
return v;
}
}