#2321
Maximum Score Of Spliced Array
expert · 1215 · lc hard +32 · verified · 58.3% accepted · 837 likes · top 54%
Description
You are given two 0-indexed integer arrays nums1 and nums2, both of length n.
You may choose integers left and right with 0 <= left <= right < n and swap the subarrays nums1[left...right] and nums2[left...right].
- For example, swapping with left = 1, right = 2 in nums1 = [1,2,3,4,5] and nums2 = [11,12,13,14,15] gives nums1 = [1,12,13,4,5] and nums2 = [11,2,3,14,15].
The swap may be applied at most once or skipped entirely.
The score equals max(sum(nums1), sum(nums2)). Return the maximum achievable score.arr[left...right] is the subarray from index left to 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.
Code
1
2
3