Skip to content

2321. Maximum Score Of Spliced Array

Difficulty Topics

Description

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] and nums2 = [11,12,13,14,15] and you choose left = 1 and right = 2, nums1 becomes [1,12,13,4,5] and nums2 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

maximum-score-of-spliced-array.py
class Solution:
    def maximumsSplicedArray(self, nums1: List[int], nums2: List[int]) -> int:
        n = len(nums1)

        def kadane(A):
            curr = res = 0

            for x in A:
                curr = max(curr + x, x)
                res = max(res, curr)

            return res

        def f(A, B):
            return sum(A) + kadane([b - a for a, b in zip(A, B)])

        return max(f(nums1, nums2), f(nums2, nums1))
maximum-score-of-spliced-array.cpp
// https://leetcode.com/discuss/interview-question/2189149/Amazon-Online-Assessment-Questions

class Solution {
public:
    vector<int> bestSplice(vector<int>& A, vector<int>& B) {
        // find the maximum subarray sum in B-A
        vector<int> ans(A.size());
        for (int i = 0; i < A.size(); i++) {
            ans[i] = B[i] - A[i];
        }

        int best = INT_MIN, start = 0, end = 0;
        int cur = 0, last = 0, cur_start = 0;
        for (int i = 0; i < ans.size(); i++) {
            if (last <= 0) cur_start = i;
            cur = ans[i] + max(last, 0);
            if (cur >= best) {
                best = cur;
                start = cur_start;
                end = i;
            }
            last = cur;
        }

        // copy over the ranges from A and B
        for (int i = 0; i < A.size(); i++) {
            ans[i] = A[i];
        }
        // only copy over the range from B if it increases the final sum
        if (best > 0) {
            for (int i = start; i <= end; i++) {
                ans[i] = B[i];
            }
        }
        return ans;
    }

    int maximumsSplicedArray(vector<int>& A, vector<int>& B) {
        vector<int> A_res = bestSplice(A, B);
        vector<int> B_res = bestSplice(B, A);

        // return the larger sum
        int A_sum = 0, B_sum = 0;
        for (int i = 0; i < A.size(); i++) {
            A_sum += A_res[i];
            B_sum += B_res[i];
        }

        return A_sum > B_sum ? A_sum : B_sum;
    }
};