Statementâ–¼
You are given two sorted arrays of distinct integers, nums1
and nums2
.
A valid path is constructed according to the following rules:
You start at the index
0 of eithernums1
ornums2
.From there, you must traverse the chosen array from left to right.
Suppose you encounter a number in both arrays (a common element, not necessarily at the same index). In that case, you may choose to switch to the other array at that element and continue the traversal from that point forward.
A common element can only be counted once in the total path score, regardless of the array it appears.
The score of a path is defined as the sum of all unique elements visited during traversal. Your task is to return the maximum possible score achievable by any valid path. As the answer may be too large, return the maximum score modulo
Constraints:
1
≤ nums1.length
,nums2.length
≤ 105 1
≤ nums1[i]
,nums2[i]
≤ 107 All elements in
nums1
andnums2
are strictly increasing.
Solution
The goal is to collect the maximum score by traversing two sorted arrays where switching between them is only allowed at common elements. The challenge is to choose when to switch to maximize the cumulative score.
This is done efficiently using the two pointers technique, which allows simultaneous traversal of both arrays in linear time, without the need to build or store all possible paths. As the arrays are strictly increasing, any prefix sum is guaranteed to grow as we move forward. At each common element, we are allowed to switch arrays. We decide whether to switch by comparing the scores accumulated from both paths and choosing the larger one as the new base to continue forward. This decision ensures that we always continue on the more rewarding path, step by step.
This approach works reliably because, beyond any common element, both arrays are still strictly increasing. So, regardless of which array we continue from, the score will continue to grow. We guarantee that the optimal path is preserved by always choosing the higher cumulative score at a common value. Let’s take an example to further understand this:
nums1
=[10,20,40,50,60] nums2
=[1,2,40,10000,35000]
At the first common element
From
sum_path1
:10+20=30 From
sum_path2
:1+2=3
As sum_path1
sum and add the common value sum_path1
and sum_path2
. This synchronization means both paths now carry the same score forward, ensuring we don’t miss high-value segments in either array, like nums2
having
Now, let’s look at the solution steps below:
Initialize two pointers,
pointer1
andpointer2
, to traversenums1
andnums2
, respectively.Initialize two running sums,
sum_path1
andsum_path2
, to track the current total score on each path.Define a constant
MOD = 10^9 + 7
to apply modulo as required by the problem constraints to prevent overflow.Traverse both arrays simultaneously using the two pointers, continuing until both pointers have reached the end of their respective arrays:
If
nums1[pointer1]
< nums2[pointer2]
:Add
nums1[pointer1]
tosum_path1
and incrementpointer1
.
Else if
nums1[pointer1]
> nums2[pointer2]
:Add
nums2[pointer2]
tosum_path2
and incrementpointer2
.
Else (if
nums1[pointer1]
== nums2[pointer2]
(common element):Add the common value to the maximum of
sum_path1
andsum_path2
.Assign this computed sum to both
sum_path1
andsum_path2
to synchronize paths.Increment both pointers to move past the common value in both arrays.
After traversal, take the maximum of
sum_path1
andsum_path2
.Return the result modulo
10^9 + 7
.
Let’s look at the following illustration to get a better understanding of the solution: