1043. Partition Array for Maximum Sum LeetCode Solution

In this guide, you will get 1043. Partition Array for Maximum Sum LeetCode Solution with the best time and space complexity. The solution to Partition Array for Maximum Sum problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Partition Array for Maximum Sum solution in C++
  4. Partition Array for Maximum Sum solution in Java
  5. Partition Array for Maximum Sum solution in Python
  6. Additional Resources
1043. Partition Array for Maximum Sum LeetCode Solution image

Problem Statement of 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] <= 109
1 <= k <= arr.length

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

1043. Partition Array for Maximum Sum LeetCode Solution in C++

class Solution {
 public:
  int maxSumAfterPartitioning(vector<int>& arr, int k) {
    const int n = arr.size();
    vector<int> dp(n + 1);

    for (int i = 1; i <= n; ++i) {
      int mx = INT_MIN;
      for (int j = 1; j <= min(i, k); ++j) {
        mx = max(mx, arr[i - j]);
        dp[i] = max(dp[i], dp[i - j] + mx * j);
      }
    }

    return dp[n];
  }
};
/* code provided by PROGIEZ */

1043. Partition Array for Maximum Sum LeetCode Solution in Java

class Solution {
  public int maxSumAfterPartitioning(int[] arr, int k) {
    final int n = arr.length;
    int[] dp = new int[n + 1];

    for (int i = 1; i <= n; ++i) {
      int mx = Integer.MIN_VALUE;
      for (int j = 1; j <= Math.max(i, k); ++j) {
        mx = Math.max(mx, arr[i - j]);
        dp[i] = Math.max(dp[i], dp[i - j] + mx * j);
      }
    }

    return dp[n];
  }
}
// code provided by PROGIEZ

1043. Partition Array for Maximum Sum LeetCode Solution in Python

class Solution:
  def maxSumAfterPartitioning(self, arr: list[int], k: int) -> int:
    n = len(arr)
    dp = [0] * (n + 1)

    for i in range(1, n + 1):
      mx = -math.inf
      for j in range(1, min(i, k) + 1):
        mx = max(mx, arr[i - j])
        dp[i] = max(dp[i], dp[i - j] + mx * j)

    return dp[n]
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.