This approach uses dynamic programming with a one-dimensional array to find the solution. We use an array dp
where dp[i]
stores the maximum sum we can get for the array arr
from the 0th index to the ith index. For each position, we try to partition the last k
elements and update the dp array accordingly, keeping track of the maximum value observed in those elements to account for possible transformations.
Time Complexity: O(n * k) since for each index, we consider up to k
previous elements.
Space Complexity: O(n) for the dp array.
1#include <stdio.h>
2#include <string.h>
3
4int maxSumAfterPartitioning(int* arr, int arrSize, int k) {
5 int dp[arrSize+1];
6 memset(dp, 0, sizeof(dp));
7 for (int i = 1; i <= arrSize; i++) {
8 int maxElem = 0, maxSum = 0;
9 for (int j = 1; j <= k && i-j >= 0; j++) {
10 maxElem = (arr[i-j] > maxElem) ? arr[i-j] : maxElem;
11 maxSum = (dp[i-j] + maxElem * j > maxSum) ? dp[i-j] + maxElem * j : maxSum;
12 }
13 dp[i] = maxSum;
14 }
15 return dp[arrSize];
16}
17
18int main() {
19 int arr[] = {1, 15, 7, 9, 2, 5, 10};
20 int k = 3;
21 int arrSize = sizeof(arr) / sizeof(arr[0]);
22 printf("%d\n", maxSumAfterPartitioning(arr, arrSize, k));
23 return 0;
24}
This C program uses a dynamic programming approach with an array dp
to compute the maximum sum achievable by partitioning the input array. The dp
array keeps track of maximum possible sums where the loop iteratively calculates maximum sums by trying all possible partitions from 1 to k
and updating dp
based on the largest found values for each segment.
In this approach, a recursive function is used to solve the problem, combined with memoization to store previously computed results. The idea is to break the problem into subproblems by recursively partitioning the array from each position and recalculating sums. Alongside recursion, memoization saves time by avoiding recomputation of results for elements already processed.
Time Complexity: O(n * k), due to exponential recursive divisions curtailed by memoization.
Space Complexity: O(n) for memoization storage.
1import java.util.Arrays;
2
3class Solution {
4 private int helper(int[] arr, int n, int k, int[] memo, int i) {
5 if (i >= n) return 0;
6 if (memo[i] != -1) return memo[i];
7 int maxElem = 0, maxSum = 0;
8 for (int j = i; j < i + k && j < n; j++) {
9 maxElem = Math.max(maxElem, arr[j]);
10 maxSum = Math.max(maxSum, maxElem * (j-i+1) + helper(arr, n, k, memo, j+1));
11 }
12 return memo[i] = maxSum;
13 }
14
15 public int maxSumAfterPartitioning(int[] arr, int k) {
16 int n = arr.length;
17 int[] memo = new int[n];
18 Arrays.fill(memo, -1);
19 return helper(arr, n, k, memo, 0);
20 }
21
22 public static void main(String[] args) {
23 Solution sol = new Solution();
24 int[] arr = {1, 15, 7, 9, 2, 5, 10};
25 int k = 3;
26 System.out.println(sol.maxSumAfterPartitioning(arr, k));
27 }
28}
Using a recursive approach embedded with memoization, the Java solution efficiently partitions arrays by caching pre-calculated partial results. The recursive process loops through allowable partitions and computes new maximum sums which are stored and reused when possible.