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.
1using System;
2
3public class Solution {
4 public int MaxSumAfterPartitioning(int[] arr, int k) {
5 int n = arr.Length;
6 int[] dp = new int[n + 1];
7 for (int i = 1; i <= n; i++) {
8 int maxElem = 0, maxSum = 0;
9 for (int j = 1; j <= k && i-j >= 0; j++) {
10 maxElem = Math.Max(maxElem, arr[i-j]);
11 maxSum = Math.Max(maxSum, dp[i-j] + maxElem * j);
12 }
13 dp[i] = maxSum;
14 }
15 return dp[n];
16 }
17
18 public static void Main(string[] args) {
19 var sol = new Solution();
20 int[] arr = {1, 15, 7, 9, 2, 5, 10};
21 int k = 3;
22 Console.WriteLine(sol.MaxSumAfterPartitioning(arr, k));
23 }
24}
This C# solution uses a dynamic programming method with a one-dimensional array. Here, dp[i]
stores the maximum sum possible for the array up to index i
. During processing, each element considers up to k
previous elements, determines the largest element in partitions, and computes the maximum sum possible.
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.
1#include <stdio.h>
2#include <limits.h>
3#include <string.h>
4#include <stdlib.h>
5
6int max(int a, int b) {
7 return a > b ? a : b;
8}
9
10int helper(int* arr, int n, int k, int* memo, int i) {
11 if (i >= n) return 0;
12 if (memo[i] != -1) return memo[i];
13 int maxElem = INT_MIN, maxSum = 0;
14 for (int j = i; j < i + k && j < n; ++j) {
15 maxElem = max(maxElem, arr[j]);
16 maxSum = max(maxSum, maxElem * (j-i+1) + helper(arr, n, k, memo, j+1));
17 }
18 return memo[i] = maxSum;
19}
20
21int maxSumAfterPartitioning(int* arr, int arrSize, int k) {
22 int* memo = (int*)malloc(arrSize * sizeof(int));
23 memset(memo, -1, arrSize * sizeof(int));
24 return helper(arr, arrSize, k, memo, 0);
25}
26
27int main() {
28 int arr[] = {1, 15, 7, 9, 2, 5, 10};
29 int k = 3;
30 int arrSize = sizeof(arr) / sizeof(arr[0]);
31 printf("%d\n", maxSumAfterPartitioning(arr, arrSize, k));
32 return 0;
33}
In this C solution, a recursive function named helper
is used to evaluate possible partitions lazily. The use of memoization, through an integer array memo
, optimizes this by caching previously calculated subproblem results. This active arrangement ensures computative processes reuse stored values during recursion.