LeetCode //C - 1031. Maximum Sum of Two Non-Overlapping Subarrays

1031. Maximum Sum of Two Non-Overlapping Subarrays

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 <= numsi <= 1000

From: LeetCode

Link: 1031. Maximum Sum of Two Non-Overlapping Subarrays


Solution:

Ideas:
  • Try both orders:
    • firstLen subarray before secondLen
    • secondLen subarray before firstLen
  • While scanning, keep:
    • the best sum of the left subarray seen so far
    • combine it with the current right subarray
Code:
c 复制代码
int maxSumTwoNoOverlap(int* nums, int numsSize, int firstLen, int secondLen) {
    int prefix[1001] = {0};
    int i;
    
    for (i = 0; i < numsSize; i++) {
        prefix[i + 1] = prefix[i] + nums[i];
    }
    
    int ans = 0;
    
    /* Case 1: firstLen subarray is before secondLen subarray */
    int bestFirst = prefix[firstLen] - prefix[0];
    for (i = firstLen + secondLen; i <= numsSize; i++) {
        int firstSum = prefix[i - secondLen] - prefix[i - secondLen - firstLen];
        if (firstSum > bestFirst) {
            bestFirst = firstSum;
        }
        
        int secondSum = prefix[i] - prefix[i - secondLen];
        int total = bestFirst + secondSum;
        if (total > ans) {
            ans = total;
        }
    }
    
    /* Case 2: secondLen subarray is before firstLen subarray */
    int bestSecond = prefix[secondLen] - prefix[0];
    for (i = firstLen + secondLen; i <= numsSize; i++) {
        int secondSum = prefix[i - firstLen] - prefix[i - firstLen - secondLen];
        if (secondSum > bestSecond) {
            bestSecond = secondSum;
        }
        
        int firstSum = prefix[i] - prefix[i - firstLen];
        int total = bestSecond + firstSum;
        if (total > ans) {
            ans = total;
        }
    }
    
    return ans;
}
相关推荐
致Great2 小时前
DeepSeek Harness插件开发实战教程:我让它自己写了一个 arXiv 搜索插件
算法
多弗朗皮卡丘4 小时前
C语言梦开始的地方7:函数
c语言·开发语言
罗西的思考6 小时前
【Agent OS / AIOS】AOHP 深度解读:当 OS 开始为 Agent 而设计
人工智能·算法·机器学习
民乐团扒谱机7 小时前
【微实验】组合优化matlab实战(马科维茨投资模型):在收益与风险之间,寻找最优的人生配比
大数据·人工智能·算法·机器学习·数学建模·matlab·组合优化
Nil2088 小时前
leetcode 160相交链表
算法·leetcode·链表
迷途之人不知返9 小时前
算法系列2:滑动窗口
算法
Herbert_hwt9 小时前
C语言零基础入门:循环控制与数据类型详解
c语言·数据结构·算法
Tyler_TXZ10 小时前
C++C语言之——二叉树
c语言·开发语言·数据结构·c++·二叉树
ZC跨境爬虫10 小时前
LeetCode 108. 将有序数组转换为二叉搜索树(递归构建详解 + Java Python 实现)
java·python·leetcode
Tisfy10 小时前
LeetCode 3090.每个字符最多出现两次的最长子字符串:二重循环 / 滑动窗口
算法·leetcode·字符串·题解·模拟·双指针·滑动窗口