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;
}
相关推荐
白狐_7981 分钟前
408 数据结构|红黑树 vs AVL:高频考点、易错判断与可能出题方式
数据结构·算法
zuozong_7 分钟前
C++类与对象
开发语言·c++·算法
是隼人10 分钟前
buuctf-pwn wustctf2020_closed(文件描述符)题解(学习过程持续更新)
c语言·学习·安全·pwn入门·ctf入门
hansang_IR11 分钟前
【代码】分层最短路模板
c++·算法·最短路
啊阿狸不会拉杆19 分钟前
《计算机网络-自顶向下方法》5.2 路由选择算法 读书笔记
计算机网络·算法·路由
rannn_11125 分钟前
【力扣hot100】多维动态规划|62、64、5、1143、72
算法·leetcode·动态规划
吴声子夜歌34 分钟前
ApacheCommons——commons-math3(科学计算与线性代数)(一)
java·线性代数·算法·apache
tachibana235 分钟前
Embedding 有哪几种算法?
人工智能·算法·ai·大模型·llm·embedding·agent
是隼人1 小时前
buuctf-pwn wdb_2018_3rd_soEasy(ret2shellcode)题解(学习过程持续更新)
c语言·学习·安全·pwn入门·ctf入门
吴声子夜歌1 小时前
ApacheCommons——commons-configuration2(多数据源配置统一管理与热加载)
java·开发语言·算法·apache