返回介绍

solution / 1100-1199 / 1191.K-Concatenation Maximum Sum / README_EN

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

1191. K-Concatenation Maximum Sum

中文文档

Description

Given an integer array arr and an integer k, modify the array by repeating it k times.

For example, if arr = [1, 2] and k = 3then the modified array will be [1, 2, 1, 2, 1, 2].

Return the maximum sub-array sum in the modified array. Note that the length of the sub-array can be 0 and its sum in that case is 0.

As the answer can be very large, return the answer modulo 109 + 7.

 

Example 1:

Input: arr = [1,2], k = 3
Output: 9

Example 2:

Input: arr = [1,-2,1], k = 5
Output: 2

Example 3:

Input: arr = [-1,-2], k = 7
Output: 0

 

Constraints:

  • 1 <= arr.length <= 105
  • 1 <= k <= 105
  • -104 <= arr[i] <= 104

Solutions

Solution 1: Prefix Sum + Case Discussion

We denote the sum of all elements in the array $arr$ as $s$, the maximum prefix sum as $mxPre$, the minimum prefix sum as $miPre$, and the maximum subarray sum as $mxSub$.

We traverse the array $arr$. For each element $x$, we update $s = s + x$, $mxPre = \max(mxPre, s)$, $miPre = \min(miPre, s)$, $mxSub = \max(mxSub, s - miPre)$.

Next, we consider the value of $k$:

  • When $k = 1$, the answer is $mxSub$.
  • When $k \ge 2$, if the maximum subarray spans two $arr$, then the answer is $mxPre + mxSuf$, where $mxSuf = s - miPre$.
  • When $k \ge 2$ and $s > 0$, if the maximum subarray spans three $arr$, then the answer is $(k - 2) \times s + mxPre + mxSuf$.

Finally, we return the result of the answer modulo $10^9 + 7$.

The time complexity is $O(n)$, and the space complexity is $O(1)$. Here, $n$ is the length of the array $arr$.

class Solution:
  def kConcatenationMaxSum(self, arr: List[int], k: int) -> int:
    s = mx_pre = mi_pre = mx_sub = 0
    for x in arr:
      s += x
      mx_pre = max(mx_pre, s)
      mi_pre = min(mi_pre, s)
      mx_sub = max(mx_sub, s - mi_pre)
    ans = mx_sub
    mod = 10**9 + 7
    if k == 1:
      return ans % mod
    mx_suf = s - mi_pre
    ans = max(ans, mx_pre + mx_suf)
    if s > 0:
      ans = max(ans, (k - 2) * s + mx_pre + mx_suf)
    return ans % mod
class Solution {
  public int kConcatenationMaxSum(int[] arr, int k) {
    long s = 0, mxPre = 0, miPre = 0, mxSub = 0;
    for (int x : arr) {
      s += x;
      mxPre = Math.max(mxPre, s);
      miPre = Math.min(miPre, s);
      mxSub = Math.max(mxSub, s - miPre);
    }
    long ans = mxSub;
    final int mod = (int) 1e9 + 7;
    if (k == 1) {
      return (int) (ans % mod);
    }
    long mxSuf = s - miPre;
    ans = Math.max(ans, mxPre + mxSuf);
    if (s > 0) {
      ans = Math.max(ans, (k - 2) * s + mxPre + mxSuf);
    }
    return (int) (ans % mod);
  }
}
class Solution {
public:
  int kConcatenationMaxSum(vector<int>& arr, int k) {
    long s = 0, mxPre = 0, miPre = 0, mxSub = 0;
    for (int x : arr) {
      s += x;
      mxPre = max(mxPre, s);
      miPre = min(miPre, s);
      mxSub = max(mxSub, s - miPre);
    }
    long ans = mxSub;
    const int mod = 1e9 + 7;
    if (k == 1) {
      return ans % mod;
    }
    long mxSuf = s - miPre;
    ans = max(ans, mxPre + mxSuf);
    if (s > 0) {
      ans = max(ans, mxPre + (k - 2) * s + mxSuf);
    }
    return ans % mod;
  }
};
func kConcatenationMaxSum(arr []int, k int) int {
  var s, mxPre, miPre, mxSub int
  for _, x := range arr {
    s += x
    mxPre = max(mxPre, s)
    miPre = min(miPre, s)
    mxSub = max(mxSub, s-miPre)
  }
  const mod = 1e9 + 7
  ans := mxSub
  if k == 1 {
    return ans % mod
  }
  mxSuf := s - miPre
  ans = max(ans, mxSuf+mxPre)
  if s > 0 {
    ans = max(ans, mxSuf+(k-2)*s+mxPre)
  }
  return ans % mod
}

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

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

发布评论

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