Imagine you have two integer arrays of equal length, and you're given a special power: you can choose any contiguous subarray from the first array and swap it with the corresponding subarray from the second array.
Your goal is to maximize the score, which is defined as the maximum sum between the two arrays after the optional swap operation.
The Challenge: Given two arrays nums1 and nums2, determine the highest possible score you can achieve by either:
- Performing exactly one subarray swap between the arrays, OR
- Leaving both arrays unchanged
Example: If nums1 = [1,2,3,4,5] and nums2 = [11,12,13,14,15], and you swap the subarray from index 1 to 2, you get:
โข nums1 = [1,12,13,4,5] (sum = 35)
โข nums2 = [11,2,3,14,15] (sum = 45)
The score would be max(35, 45) = 45
Input & Output
Visualization
Time & Space Complexity
Single pass through arrays to apply Kadane's algorithm
Only using constant extra variables
Constraints
- n == nums1.length == nums2.length
- 1 โค n โค 105
- -104 โค nums1[i], nums2[i] โค 104