Sponsored
Sponsored
This approach uses a prefix sum array to quickly calculate the sum of any subarray and a sliding window technique to explore possible starting points for the subarrays.
Time Complexity: O(n), since we make a constant number of passes through the array.
Space Complexity: O(n), due to the prefix, left, and right arrays.
1#include <iostream>
2#include <vector>
3using namespace std;
4
5vector<int> maxSumOfThreeSubarrays(vector<int>& nums, int k) {
6 int n = nums.size();
7 vector<int> prefix(n + 1, 0);
8 for (int i = 0; i < n; i++) {
9 prefix[i + 1] = prefix[i] + nums[i];
10 }
11 vector<int> left(n, 0), right(n, n - k);
12 for (int i = k, total = prefix[k] - prefix[0]; i < n; i++) {
13 if (prefix[i + 1] - prefix[i + 1 - k] > total) {
14 left[i] = i + 1 - k;
15 total = prefix[i + 1] - prefix[i + 1 - k];
16 } else {
17 left[i] = left[i - 1];
18 }
19 }
20 for (int i = n - k - 1, total = prefix[n] - prefix[n - k]; i >= 0; i--) {
21 if (prefix[i + k] - prefix[i] >= total) {
22 right[i] = i;
23 total = prefix[i + k] - prefix[i];
24 } else {
25 right[i] = right[i + 1];
26 }
27 }
28 vector<int> ans(3, 0);
29 int max_sum = 0;
30 for (int i = k; i <= n - 2 * k; ++i) {
31 int l = left[i - 1], r = right[i + k];
32 int current_sum = prefix[l + k] - prefix[l] + prefix[i + k] - prefix[i] + prefix[r + k] - prefix[r];
33 if (current_sum > max_sum) {
34 max_sum = current_sum;
35 ans = {l, i, r};
36 }
37 }
38 return ans;
39}
40
41int main() {
42 vector<int> nums = {1, 2, 1, 2, 6, 7, 5, 1};
43 int k = 2;
44 vector<int> result = maxSumOfThreeSubarrays(nums, k);
45 for (int i : result) {
46 cout << i << " ";
47 }
48 return 0;
49}
The C++ solution implements the prefix sum strategy with two additional arrays (left
, right
) to efficiently calculate possible subarray sums. It iterates over each potential middle subarray to find the maximum total sum, using pre-calculated best subarrays from both sides.
This approach employs dynamic programming alongside a sliding window to optimize subarray sum calculations and ensure non-overlapping conditions.
Time Complexity: O(n), for traversal of the nums array multiple times.
Space Complexity: O(n), utilizing the dp, prefix, left, and right arrays.
1
public class Solution {
public int[] MaxSumOfThreeSubarraysDP(int[] nums, int k) {
int n = nums.Length;
int[] prefix = new int[n + 1];
int[] dp = new int[n];
int[] left = new int[n];
int[] right = new int[n];
for (int i = 0; i < n; ++i) {
prefix[i + 1] = prefix[i] + nums[i];
}
for (int i = k; i < n; ++i) {
if (prefix[i + 1] - prefix[i + 1 - k] > (dp[i - 1] if i - 1 >= 0 else 0)) {
dp[i] = prefix[i + 1] - prefix[i + 1 - k];
left[i] = i + 1 - k;
} else {
dp[i] = dp[i - 1];
left[i] = left[i - 1];
}
}
right[n - k] = n - k;
for (int i = n - k - 1; i >= 0; --i) {
if (prefix[i + k] - prefix[i] >= prefix[right[i + 1] + k] - prefix[right[i + 1]]) {
right[i] = i;
} else {
right[i] = right[i + 1];
}
}
int maxSum = 0;
int[] result = new int[3];
for (int i = k; i <= n - 2 * k; ++i) {
int l = left[i - 1];
int r = right[i + k];
int totalSum = (prefix[i + k] - prefix[i]) + (prefix[l + k] - prefix[l]) + (prefix[r + k] - prefix[r]);
if (totalSum > maxSum) {
maxSum = totalSum;
result[0] = l;
result[1] = i;
result[2] = r;
}
}
return result;
}
public static void Main(string[] args) {
Solution solution = new Solution();
int[] nums = {1, 2, 1, 2, 6, 7, 5, 1};
int k = 2;
int[] result = solution.MaxSumOfThreeSubarraysDP(nums, k);
Console.WriteLine(string.Join(", ", result));
}
}
In C#, the code uses dynamic programming to manage cumulative sums, leveraging strategic indexing through left
and right
tracking arrays. This captures the strongest results with efficient stored sum calculations, thus finding optimal subarray triplets.