-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmaximum_sum_of_two_non_overlapping_subarrays.cpp
More file actions
33 lines (32 loc) · 1.37 KB
/
maximum_sum_of_two_non_overlapping_subarrays.cpp
File metadata and controls
33 lines (32 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// 两个非重叠子数组的最大和
// https://leetcode.cn/problems/maximum-sum-of-two-non-overlapping-subarrays
// INLINE ../../images/array/maximum_sum_of_two_non_overlapping_subarrays.jpeg
#include <headers.hpp>
class Solution {
public:
int maxSumTwoNoOverlap(vector<int> &nums, int firstLen, int secondLen) {
int n = nums.size(); // 数组长度
vector<int> prefixSum(n + 1, 0); // 前缀和数组
for (int i = 0; i < n; ++i) {
prefixSum[i + 1] = prefixSum[i] + nums[i]; // 计算前缀和
}
int res = 0;
// 枚举第一个子数组的起始位置和第二个子数组的起始位置
for (int i = 0; i + firstLen + secondLen <= n; ++i) {
for (int j = i + firstLen; j + secondLen <= n; ++j) {
// 计算两个子数组的和,并更新最大值
res = max(res, prefixSum[i + firstLen] - prefixSum[i] +
prefixSum[j + secondLen] - prefixSum[j]);
}
}
// 枚举第二个子数组的起始位置和第一个子数组的起始位置
for (int i = 0; i + firstLen + secondLen <= n; ++i) {
for (int j = i + secondLen; j + firstLen <= n; ++j) {
// 计算两个子数组的和,并更新最大值
res = max(res, prefixSum[i + secondLen] - prefixSum[i] +
prefixSum[j + firstLen] - prefixSum[j]);
}
}
return res; // 返回最大和
}
};