Get the Maximum Score - Problem
Path Navigation with Maximum Score

You are given two sorted arrays of distinct integers nums1 and nums2. Your goal is to find a path through these arrays that maximizes the sum of values you collect.

🛣️ Path Rules:
  • Start at index 0 of either array
  • Move left-to-right within your current array
  • When you encounter a value that exists in both arrays, you can switch paths
  • Each unique value is counted only once in your score

Example: If nums1 = [2,4,5,8,10] and nums2 = [4,6,8,9], you could take path: 2 → 4 → 6 → 8 → 9 for score = 29, or 2 → 4 → 5 → 8 → 10 for score = 29.

Return the maximum possible score modulo 109 + 7.

Input & Output

example_1.py — Basic Path Selection
$ Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9]
Output: 30
💡 Note: Optimal path: 2 → 4 → 6 → 8 → 10. At intersection 4, both paths have equal sum (2 vs 0), so we can choose either. At intersection 8, path through nums2 gives us 4+6=10 vs nums1's 4+5=9, so we switch to nums2, then continue to get the maximum ending.
example_2.py — Multiple Intersections
$ Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100]
Output: 109
💡 Note: Optimal path: 1 → 3 → 5 → 100. Start with nums1 to get 1, then at intersection 3, nums1 path has sum 1 vs nums2's 0, so continue on nums1. At intersection 5, both have equal recent sums, but we can switch to nums2 to get the large value 100.
example_3.py — No Intersections
$ Input: nums1 = [1,4,5,8,9], nums2 = [2,3,6,7]
Output: 35
💡 Note: Since there are no common elements, we must choose one complete array. nums1 sum = 1+4+5+8+9 = 27, nums2 sum = 2+3+6+7 = 18, so we choose nums1 entirely for a total of 27.

Constraints

  • 1 ≤ nums1.length, nums2.length ≤ 105
  • 1 ≤ nums1[i], nums2[i] ≤ 107
  • nums1 and nums2 are strictly increasing
  • All elements in each array are distinct

Visualization

Tap to expand
Get the Maximum Score INPUT nums1 (sorted): 2 4 5 8 10 nums2 (sorted): 4 6 8 9 Common values (switch points) Two Paths with Switch Points: Path 1 Path 2 4 8 Can switch at 4 and 8 Goal: Maximize sum on path ALGORITHM STEPS 1 Two Pointers Track sum1, sum2 for each segment between switches 2 Compare Elements If nums1[i] < nums2[j]: add to sum1 If nums1[i] > nums2[j]: add to sum2 3 At Common Value result += max(sum1, sum2) Add common value, reset sums 4 Final Merge Add remaining elements Return result % (10^9 + 7) Segment Sums: Seg 1: [2] vs [] --> max(2,0)=2 At 4: add 4 --> total=6 Seg 2: [5] vs [6] --> max(5,6)=6 At 8: add 8 --> total=20 Seg 3: [10] vs [9] --> max(10,9)=10 Total: 2+4+6+8+10 = 30 FINAL RESULT Optimal Path Taken: 2 4 5 8 10 4 6 8 9 Chosen in path Skipped Sum Breakdown: Path 1 start: 2 Switch at 4: 4 Take from path 2: 6 Switch at 8: 8, then 10 Maximum Score: 30 (2+4+6+8+10 = 30) Key Insight: Common values act as "bridge points" where you can switch between arrays. Between bridges, always take the path with higher sum. Use two pointers to traverse both arrays in O(n+m) time. At each common value, add max(sum1, sum2) + common_value, then reset both sums to 0. TutorialsPoint - Get the Maximum Score | Two Pointers with Greedy Approach
Asked in
Google 42 Amazon 38 Meta 31 Microsoft 25
41.2K Views
High Frequency
~25 min Avg. Time
1.2K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen