返回介绍

solution / 1000-1099 / 1031.Maximum Sum of Two Non-Overlapping Subarrays / README_EN

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

1031. Maximum Sum of Two Non-Overlapping Subarrays

中文文档

Description

Given an integer array nums and two integers firstLen and secondLen, return _the maximum sum of elements in two non-overlapping subarrays with lengths _firstLen_ and _secondLen.

The array with length firstLen could occur before or after the array with length secondLen, but they have to be non-overlapping.

A subarray is a contiguous part of an array.

 

Example 1:

Input: nums = [0,6,5,2,2,5,1,9,4], firstLen = 1, secondLen = 2
Output: 20
Explanation: One choice of subarrays is [9] with length 1, and [6,5] with length 2.

Example 2:

Input: nums = [3,8,1,3,2,1,8,9,0], firstLen = 3, secondLen = 2
Output: 29
Explanation: One choice of subarrays is [3,8,1] with length 3, and [8,9] with length 2.

Example 3:

Input: nums = [2,1,5,6,0,9,5,0,3,8], firstLen = 4, secondLen = 3
Output: 31
Explanation: One choice of subarrays is [5,6,0,9] with length 4, and [0,3,8] with length 3.

 

Constraints:

  • 1 <= firstLen, secondLen <= 1000
  • 2 <= firstLen + secondLen <= 1000
  • firstLen + secondLen <= nums.length <= 1000
  • 0 <= nums[i] <= 1000

Solutions

Solution 1

class Solution:
  def maxSumTwoNoOverlap(self, nums: List[int], firstLen: int, secondLen: int) -> int:
    n = len(nums)
    s = list(accumulate(nums, initial=0))
    ans = t = 0
    i = firstLen
    while i + secondLen - 1 < n:
      t = max(t, s[i] - s[i - firstLen])
      ans = max(ans, t + s[i + secondLen] - s[i])
      i += 1
    t = 0
    i = secondLen
    while i + firstLen - 1 < n:
      t = max(t, s[i] - s[i - secondLen])
      ans = max(ans, t + s[i + firstLen] - s[i])
      i += 1
    return ans
class Solution {
  public int maxSumTwoNoOverlap(int[] nums, int firstLen, int secondLen) {
    int n = nums.length;
    int[] s = new int[n + 1];
    for (int i = 0; i < n; ++i) {
      s[i + 1] = s[i] + nums[i];
    }
    int ans = 0;
    for (int i = firstLen, t = 0; i + secondLen - 1 < n; ++i) {
      t = Math.max(t, s[i] - s[i - firstLen]);
      ans = Math.max(ans, t + s[i + secondLen] - s[i]);
    }
    for (int i = secondLen, t = 0; i + firstLen - 1 < n; ++i) {
      t = Math.max(t, s[i] - s[i - secondLen]);
      ans = Math.max(ans, t + s[i + firstLen] - s[i]);
    }
    return ans;
  }
}
class Solution {
public:
  int maxSumTwoNoOverlap(vector<int>& nums, int firstLen, int secondLen) {
    int n = nums.size();
    vector<int> s(n + 1);
    for (int i = 0; i < n; ++i) {
      s[i + 1] = s[i] + nums[i];
    }
    int ans = 0;
    for (int i = firstLen, t = 0; i + secondLen - 1 < n; ++i) {
      t = max(t, s[i] - s[i - firstLen]);
      ans = max(ans, t + s[i + secondLen] - s[i]);
    }
    for (int i = secondLen, t = 0; i + firstLen - 1 < n; ++i) {
      t = max(t, s[i] - s[i - secondLen]);
      ans = max(ans, t + s[i + firstLen] - s[i]);
    }
    return ans;
  }
};
func maxSumTwoNoOverlap(nums []int, firstLen int, secondLen int) (ans int) {
  n := len(nums)
  s := make([]int, n+1)
  for i, x := range nums {
    s[i+1] = s[i] + x
  }
  for i, t := firstLen, 0; i+secondLen-1 < n; i++ {
    t = max(t, s[i]-s[i-firstLen])
    ans = max(ans, t+s[i+secondLen]-s[i])
  }
  for i, t := secondLen, 0; i+firstLen-1 < n; i++ {
    t = max(t, s[i]-s[i-secondLen])
    ans = max(ans, t+s[i+firstLen]-s[i])
  }
  return
}

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

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

发布评论

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