返回介绍

solution / 1000-1099 / 1043.Partition Array for Maximum Sum / README_EN

发布于 2024-06-17 01:03:31 字数 4568 浏览 0 评论 0 收藏 0

1043. Partition Array for Maximum Sum

中文文档

Description

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

Solutions

Solution 1: Dynamic Programming

We define $f[i]$ to represent the maximum element sum of the first $i$ elements of the array after separating them into several subarrays. At the beginning, $f[i]=0$, and the answer is $f[n]$.

We consider how to calculate $f[i]$, where $i \geq 1$.

For $f[i]$, its last element is $arr[i-1]$. Since the maximum length of each subarray is $k$, and we need to find the maximum value in the subarray, we can enumerate the first element $arr[j - 1]$ of the last subarray from right to left, where $\max(0, i - k) \lt j \leq i$, and maintain a variable $mx$ during the process to represent the maximum value in the subarray. The state transition equation is:

$$ f[i] = \max{f[i], f[j - 1] + mx \times (i - j + 1)} $$

The final answer is $f[n]$.

The time complexity is $O(n \times k)$, and the space complexity is $O(n)$, where $n$ is the length of the array $arr$.

class Solution:
  def maxSumAfterPartitioning(self, arr: List[int], k: int) -> int:
    n = len(arr)
    f = [0] * (n + 1)
    for i in range(1, n + 1):
      mx = 0
      for j in range(i, max(0, i - k), -1):
        mx = max(mx, arr[j - 1])
        f[i] = max(f[i], f[j - 1] + mx * (i - j + 1))
    return f[n]
class Solution {
  public int maxSumAfterPartitioning(int[] arr, int k) {
    int n = arr.length;
    int[] f = new int[n + 1];
    for (int i = 1; i <= n; ++i) {
      int mx = 0;
      for (int j = i; j > Math.max(0, i - k); --j) {
        mx = Math.max(mx, arr[j - 1]);
        f[i] = Math.max(f[i], f[j - 1] + mx * (i - j + 1));
      }
    }
    return f[n];
  }
}
class Solution {
public:
  int maxSumAfterPartitioning(vector<int>& arr, int k) {
    int n = arr.size();
    int f[n + 1];
    memset(f, 0, sizeof(f));
    for (int i = 1; i <= n; ++i) {
      int mx = 0;
      for (int j = i; j > max(0, i - k); --j) {
        mx = max(mx, arr[j - 1]);
        f[i] = max(f[i], f[j - 1] + mx * (i - j + 1));
      }
    }
    return f[n];
  }
};
func maxSumAfterPartitioning(arr []int, k int) int {
  n := len(arr)
  f := make([]int, n+1)
  for i := 1; i <= n; i++ {
    mx := 0
    for j := i; j > max(0, i-k); j-- {
      mx = max(mx, arr[j-1])
      f[i] = max(f[i], f[j-1]+mx*(i-j+1))
    }
  }
  return f[n]
}
function maxSumAfterPartitioning(arr: number[], k: number): number {
  const n: number = arr.length;
  const f: number[] = new Array(n + 1).fill(0);
  for (let i = 1; i <= n; ++i) {
    let mx: number = 0;
    for (let j = i; j > Math.max(0, i - k); --j) {
      mx = Math.max(mx, arr[j - 1]);
      f[i] = Math.max(f[i], f[j - 1] + mx * (i - j + 1));
    }
  }
  return f[n];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文