-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPartitionArray.java
More file actions
31 lines (27 loc) · 829 Bytes
/
PartitionArray.java
File metadata and controls
31 lines (27 loc) · 829 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
31
package org.sean.dynamicpro;
/***
* 1043. Partition Array for Maximum Sum
*/
public class PartitionArray {
public int maxSumAfterPartitioning(int[] arr, int k) {
int len = arr.length;
int[] sums = new int[len];
int maxSofar = -1;
for (int i = 0; i < k; i++) {
maxSofar = Math.max(arr[i], maxSofar);
sums[i] = (i + 1) * maxSofar;
}
for (int j = k; j < len; j++) {
int max = -1;
// think about the formed group end position at j
// xxx {j}
// xx {xj}
// ...
for (int t = 0; t < k; t++) {
max = Math.max(max, arr[j - t]);
sums[j] = Math.max(sums[j], max * (t + 1) + sums[j - 1 - t]);
}
}
return sums[len - 1];
}
}