Maximum Score Of Spliced Array LeetCode solution in C++

1
class Solution {
2
public:
3
int maximumsSplicedArray(vector<int> &nums1, vector<int> &nums2) {
4
return max(MaxValOfA1(nums1, nums2), MaxValOfA1(nums2, nums1));
5
}
6

7
int MaxValOfA1(vector<int> &a1, vector<int> &a2) {
8
int n = a1.size();
9
int sum = 0;
10
for (auto &i : a1) sum += i;
11
int L = 0, R = 0, max_inc = 0;
12
// max_inc is maximum positive contribution of a1
13
int acc1 = 0, acc2 = 0;
14
while (L < n && R < n) {
15
acc1 += a1[R];
16
acc2 += a2[R];
17
max_inc = max(max_inc, acc2 - acc1);
18
if (acc1 >= acc2) {
19
L = R + 1;
20
R = L;
21
acc1 = 0;
22
acc2 = 0;
23
} else {
24
R = R + 1;
25
}
26
}
27
return sum + max_inc;
28
}
29
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0