This repository was archived by the owner on Mar 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.cpp
59 lines (51 loc) · 1.58 KB
/
solution.cpp
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class Solution {
public:
vector<int> maxSumOfThreeSubarrays(vector<int> &nums, int k) {
int n = nums.size();
vector<int> sums(n - k + 1); // Store sums of all possible k-sized windows
// Calculate sum of first window
int sum = 0;
for (int i = 0; i < k; i++) {
sum += nums[i];
}
sums[0] = sum;
// Calculate sums for remaining windows
for (int i = k; i < n; i++) {
sum = sum + nums[i] - nums[i - k];
sums[i - k + 1] = sum;
}
// left[i] stores the index of max sum subarray from left to i
vector<int> left(sums.size(), 0);
int best = 0;
for (int i = 0; i < sums.size(); i++) {
if (sums[i] > sums[best]) {
best = i;
}
left[i] = best;
}
// right[i] stores the index of max sum subarray from i to end
vector<int> right(sums.size(), 0);
best = sums.size() - 1;
for (int i = sums.size() - 1; i >= 0; i--) {
if (sums[i] >= sums[best]) { // >= for lexicographically smallest
best = i;
}
right[i] = best;
}
// Find the best combination of three subarrays
vector<int> result = {0, k,
k * 2}; // Initialize with first possible position
int maxSum = 0;
// Try all possible middle positions
for (int i = k; i <= n - 2 * k; i++) {
int l = left[i - k]; // Best position for left subarray
int r = right[i + k]; // Best position for right subarray
int total = sums[l] + sums[i] + sums[r];
if (total > maxSum) {
maxSum = total;
result = {l, i, r};
}
}
return result;
}
};