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.
1def maxSumOfThreeSubarrays(nums, k):
2 n = len(nums)
3 prefix = [0] * (n + 1)
4 for i in range(1, n + 1):
5 prefix[i] = prefix[i - 1] + nums[i - 1]
6 left = [0] * n
7 right = [0] * n
8 for i in range(k, n):
9 if prefix[i + 1] - prefix[i + 1 - k] > prefix[left[i - 1] + k] - prefix[left[i - 1]]:
10 left[i] = i + 1 - k
11 else:
12 left[i] = left[i - 1]
13 for i in range(n - k - 1, -1, -1):
14 if prefix[i + k] - prefix[i] >= prefix[right[i + 1] + k] - prefix[right[i + 1]]:
15 right[i] = i
16 else:
17 right[i] = right[i + 1]
18 max_sum = 0
19 ans = [-1, -1, -1]
20 for mid in range(k, n - 2 * k + 1):
21 l, r = left[mid - 1], right[mid + k]
22 total_sum = (prefix[l + k] - prefix[l]) + (prefix[mid + k] - prefix[mid]) + (prefix[r + k] - prefix[r])
23 if total_sum > max_sum:
24 max_sum = total_sum
25 ans = [l, mid, r]
26 return ans
27
28# Example usage
29nums = [1, 2, 1, 2, 6, 7, 5, 1]
30k = 2
31print(maxSumOfThreeSubarrays(nums, k))
This Python solution computes prefix sums to simplify subarray calculations. It keeps track of the best possible subarray starting indices for the left and right subarrays. The middle subarray iterates to check for the highest achievable sum combining left, middle, and right subarrays. The process carefully maintains indices for lexicographical order.
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#include <vector>
using namespace std;
vector<int> maxSumOfThreeSubarraysDP(vector<int>& nums, int k) {
int n = nums.size();
vector<int> dp(n, 0);
vector<int> prefix(n + 1, 0);
vector<int> left(n), right(n);
for (int i = 0; i < n; ++i) {
prefix[i + 1] = prefix[i] + nums[i];
}
for (int i = 0; i < k; ++i) {
dp[i] = prefix[i + 1];
}
for (int i = k; i < n; ++i) {
if (prefix[i + 1] - prefix[i + 1 - k] >= dp[i - 1]) {
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];
}
}
vector<int> result(3);
int maxSum = 0;
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 = {l, i, r};
}
}
return result;
}
int main() {
vector<int> nums = {1, 2, 1, 2, 6, 7, 5, 1};
int k = 2;
vector<int> result = maxSumOfThreeSubarraysDP(nums, k);
for (int i : result) {
cout << i << " ";
}
return 0;
}
The C++ implementation uses dynamic programming to store the cumulative sums and utilizes left
and right
arrays to preserve optimal positions for subarray ranges. This allows efficiently choosing and maintaining the best sums for the problem's conditions.