You are given two 0-indexed integer arrays nums1
and nums2
, both of length n
.
You can choose two integers left
and right
where 0 <= left <= right < n
and swap the subarray nums1[left...right]
with the subarray nums2[left...right]
.
- For example, if
nums1 = [1,2,3,4,5]
andnums2 = [11,12,13,14,15]
and you chooseleft = 1
andright = 2
,nums1
becomes[1,12,13,4,5]
andnums2
becomes[11,2,3,14,15]
.
You may choose to apply the mentioned operation once or not do anything.
The score of the arrays is the maximum of sum(nums1)
and sum(nums2)
, where sum(arr)
is the sum of all the elements in the array arr
.
Return the maximum possible score.
A subarray is a contiguous sequence of elements within an array. arr[left...right]
denotes the subarray that contains the elements of nums
between indices left
and right
(inclusive).
Example 1:
Input: nums1 = [60,60,60], nums2 = [10,90,10] Output: 210 Explanation: Choosing left = 1 and right = 1, we have nums1 = [60,90,60] and nums2 = [10,60,10]. The score is max(sum(nums1), sum(nums2)) = max(210, 80) = 210.
Example 2:
Input: nums1 = [20,40,20,70,30], nums2 = [50,20,50,40,20] Output: 220 Explanation: Choosing left = 3, right = 4, we have nums1 = [20,40,20,40,20] and nums2 = [50,20,50,70,30]. The score is max(sum(nums1), sum(nums2)) = max(140, 220) = 220.
Example 3:
Input: nums1 = [7,11,13], nums2 = [1,1,1] Output: 31 Explanation: We choose not to swap any subarray. The score is max(sum(nums1), sum(nums2)) = max(31, 3) = 31.
Constraints:
n == nums1.length == nums2.length
1 <= n <= 105
1 <= nums1[i], nums2[i] <= 104
Solution
Explanation
max(B’) = sum(B) + kadane(A – B)
max(A’) = sum(A) + kadane(B – A)
Complexity
Time O(n)
Space O(1)
def maximumsSplicedArray(self, A, B):
def kadane(A, B):
res = cur = 0
for i in range(len(A)):
cur = max(0, cur + A[i] - B[i])
res = max(res, cur)
return res + sum(B)
return max(kadane(A, B), kadane(B, A))
int kadane(vector<int>& n1, vector<int>& n2) {
int sz = n1.size(), sum = 0, res = 0;
for (int i = 0; i < sz; ++i) {
sum = max(n2[i] - n1[i], sum + n2[i] - n1[i]);
res = max(res, sum);
}
return res;
}
int maximumsSplicedArray(vector<int>& n1, vector<int>& n2) {
return max(accumulate(begin(n1), end(n1), 0) + kadane(n1, n2),
accumulate(begin(n2), end(n2), 0) + kadane(n2, n1));
}
or we can do it in one pass.
int maximumsSplicedArray(vector<int>& n1, vector<int>& n2) {
int kd[2] = {}, res[2] = {}, sum[2] = {};
for (int i = 0; i < n1.size(); ++i) {
kd[0] = max(n2[i] - n1[i], kd[0] + n2[i] - n1[i]);
res[0] = max(res[0], kd[0]);
kd[1] = max(n1[i] - n2[i], kd[1] + n1[i] - n2[i]);
res[1] = max(res[1], kd[1]);
sum[0] += n1[i];
sum[1] += n2[i];
}
return max(sum[0] + res[0], sum[1] + res[1]);
}
public int maximumsSplicedArray(int[] nums1, int[] nums2) {
int s1 = Arrays.stream(nums1).sum();
int s2 = Arrays.stream(nums2).sum();
int[] v1 = new int[nums1.length], v2 = new int[nums1.length];
for (int i = 0; i < nums1.length; i++) {
v1[i] = nums1[i] - nums2[i];
v2[i] = nums2[i] - nums1[i];
}
return Math.max(s1 + maxSubArray(v2), s2 + maxSubArray(v1));
}
private int maxSubArray(int[] nums) {
int res = Integer.MIN_VALUE, m = 0;
for (int x: nums) {
m = Math.max(x, m + x);
res = Math.max(res, m);
}
return res;
}
Happy Learning – If you require any further information, feel free to contact me.