-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1043-PartitionArrayForMaximumSum.go
More file actions
61 lines (51 loc) · 1.43 KB
/
1043-PartitionArrayForMaximumSum.go
File metadata and controls
61 lines (51 loc) · 1.43 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
package main
import "fmt"
// 1043. Partition Array for Maximum Sum
// Given an integer array arr, partition the array into (contiguous) subarrays of length at most k.
// After partitioning, each subarray has their values changed to become the maximum value of that subarray.
// Return the largest sum of the given array after partitioning.
// Test cases are generated so that the answer fits in a 32-bit integer.
// Example 1:
// Input: arr = [1,15,7,9,2,5,10], k = 3
// Output: 84
// Explanation: arr becomes [15,15,15,9,10,10,10]
// Example 2:
// Input: arr = [1,4,1,5,7,3,6,1,9,9,3], k = 4
// Output: 83
// Example 3:
// Input: arr = [1], k = 1
// Output: 1
// Constraints:
// 1 <= arr.length <= 500
// 0 <= arr[i] <= 10^9
// 1 <= k <= arr.length
func maxSumAfterPartitioning(arr []int, k int) int {
l := len(arr);
dp := make([]int,l)
dp[0] = arr[0];
for i := 1; i < l; i = i + 1 {
j := i
maxInPart := arr[j]
for ; j > i - k && j >= 0; j = j - 1 {
maxInPart = max(maxInPart,arr[j]);
prev := 0
if j - 1 >= 0 {
prev = dp[j - 1]
}
dp[i] = max(dp[i], prev + maxInPart * (i - j + 1))
fmt.Println(dp)
}
}
return dp[l - 1]
}
func max(a, b int) int {
if a <= b {
return b
}
return a
}
func main() {
fmt.Println(maxSumAfterPartitioning([]int{1,15,7,9,2,5,10}, 3)) // 84
fmt.Println(maxSumAfterPartitioning([]int{1,4,1,5,7,3,6,1,9,9,3}, 4)) // 83
fmt.Println(maxSumAfterPartitioning([]int{1}, 1)) // 1
}