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.
1using System;
2
3public class Solution {
4 public int[] MaxSumOfThreeSubarrays(int[] nums, int k) {
5 int n = nums.Length;
6 int[] prefix = new int[n + 1];
7 for (int i = 0; i < n; i++) {
8 prefix[i + 1] = prefix[i] + nums[i];
9 }
10 int[] left = new int[n];
11 int[] right = new int[n];
12 for (int i = k, total = prefix[k] - prefix[0]; i < n; i++) {
13 if (prefix[i + 1] - prefix[i + 1 - k] > total) {
14 total = prefix[i + 1] - prefix[i + 1 - k];
15 left[i] = 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 total = prefix[i + k] - prefix[i];
23 right[i] = i;
24 } else {
25 right[i] = right[i + 1];
26 }
27 }
28 int[] ans = new int[3];
29 int maxSum = 0;
30 for (int i = k; i <= n - 2 * k; ++i) {
31 int l = left[i - 1];
32 int r = right[i + k];
33 int currentSum = prefix[i + k] - prefix[i] + prefix[l + k] - prefix[l] + prefix[r + k] - prefix[r];
34 if (currentSum > maxSum) {
35 maxSum = currentSum;
36 ans[0] = l;
37 ans[1] = i;
38 ans[2] = r;
39 }
40 }
41 return ans;
42 }
43
44 public static void Main(string[] args) {
45 Solution solution = new Solution();
46 int[] nums = {1, 2, 1, 2, 6, 7, 5, 1};
47 int k = 2;
48 int[] result = solution.MaxSumOfThreeSubarrays(nums, k);
49 Console.WriteLine(string.Join(", ", result));
50 }
51}
This C# solution combines prefix sums and sliding window principles to determine the maximum subarray sums for three non-overlapping sections within the input list. By maintaining correct indices for left and right subarrays, it efficiently finds the middle subarray to maximize the total sum.
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.