Get the Maximum Score Problem
Description
LeetCode Problem 1537.
You are given two sorted arrays of distinct integers nums1 and nums2.
A valid path is defined as follows:
- Choose array
nums1ornums2to traverse (from index-0). - Traverse the current array from left to right.
- If you are reading any value that is present in
nums1andnums2you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path).
The score is defined as the sum of unique values in a valid path.
Return the maximum score you can obtain of all possible valid paths. Since the answer may be too large, return it modulo 10^9 + 7.
Example 1:

Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9]
Output: 30
Explanation: Valid paths:
[2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1)
[4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2)
The maximum is obtained with the path in green [2,4,6,8,10].
Example 2:
Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100]
Output: 109
Explanation: Maximum sum is obtained with the path [1,3,5,100].
Example 3:
Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10]
Output: 40
Explanation: There are no common elements between nums1 and nums2.
Maximum sum is obtained with the path [6,7,8,9,10].
Constraints:
1 <= nums1.length, nums2.length <= 10^51 <= nums1[i], nums2[i] <= 10^7nums1andnums2are strictly increasing.
Solution
Approach: Two Pointers Between Shared Values
The shared values split both arrays into segments. Between two consecutive shared values, a path stays on one array, and at each shared value it may switch. So in each segment we should simply take the array whose segment has the larger sum.
Merge the two sorted arrays with pointers i and j, keeping two running sums: best1, the best score of a path that has just read nums1[i - 1], and best2 for nums2.
- If
nums1[i]is smaller (ornums2is used up), add it tobest1and movei. - If
nums2[j]is smaller (ornums1is used up), add it tobest2and movej. - If they are equal, both paths reach the same value. Whichever path we took so far, we can continue on either array from here, so set both sums to
max(best1, best2) + nums1[i]and move both pointers. The shared value is counted once.
The answer is max(best1, best2) at the end. The tricky part is the modulo: the score can reach about 2 * 1012, so keep the sums in 64-bit integers and take % (10^9 + 7) only once, at the end. Taking it earlier would break the max comparisons.
Complexity: O(m + n) time and O(1) extra space.
class Solution:
def maxSum(self, nums1: List[int], nums2: List[int]) -> int:
MOD = 10**9 + 7
m, n = len(nums1), len(nums2)
i = j = 0
# best1 / best2 = best score of a path that has just read nums1[i - 1] / nums2[j - 1]
best1 = best2 = 0
while i < m or j < n:
if j == n or (i < m and nums1[i] < nums2[j]):
best1 += nums1[i]
i += 1
elif i == m or nums1[i] > nums2[j]:
best2 += nums2[j]
j += 1
else:
# A shared value: both paths meet here, so both continue from the better one.
best1 = best2 = max(best1, best2) + nums1[i]
i += 1
j += 1
return max(best1, best2) % MOD # take the modulo only at the end, never while comparing