LeetCode Problem

How to Solve Get the Maximum Score

The 'Get the Maximum Score' problem is solved using dynamic programming to partition arrays by common elements. The goal is to find a valid path between two arrays that maximizes the score by selecting the sum of unique values. A key optimization strategy involves choosing the larger sum path at each step using a state transition approach.

GhostInterview Help

Need help with Get the Maximum Score without spending extra time grinding it?

GhostInterview can read Get the Maximum Score from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #1537State transition dynamic programmingReviewed 2026-03-08
Difficulty
Hard
Primary pattern
State transition dynamic programming
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

The 'Get the Maximum Score' problem is solved using dynamic programming to partition arrays by common elements. The goal is to find a valid path between two arrays that maximizes the score by selecting the sum of unique values. A key optimization strategy involves choosing the larger sum path at each step using a state transition approach.

Problem Statement

You are given two sorted arrays of distinct integers, nums1 and nums2. A valid path between these arrays is defined by selecting elements starting from the beginning of either array, and at each step, moving to the next element from the same array or switching to the other array if they share a common value.

The score of a valid path is the sum of the distinct integers in the path. The task is to find the path that maximizes this score. The arrays nums1 and nums2 have lengths between 1 and 100,000 and their elements range from 1 to 10 million. Both arrays are strictly increasing.

Examples

Example 1

Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9]

Output: 30

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

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

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 <= 105
  • 1 <= nums1[i], nums2[i] <= 107
  • nums1 and nums2 are strictly increasing.

Solution Approach

Dynamic Programming with Partitioning

Use dynamic programming to keep track of the maximum score at each position in both arrays. Partition the arrays by their common elements and calculate the best path by choosing the largest sum at each step. Transition between states to compute the score for paths starting from either array.

Two Pointers Technique

Implement a two-pointer approach to traverse both arrays simultaneously. At each step, compare the elements from both arrays, move the pointer for the smaller element, and update the maximum score by selecting the larger possible sum path.

Efficient State Transition

Transition between states when encountering common elements. At each common element, evaluate whether it's better to continue from the current array or switch arrays. This decision will depend on which path yields a higher score.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

The time complexity depends on the approach used. The two-pointer technique ensures that each element from both arrays is visited only once, yielding a time complexity of O(n + m), where n and m are the lengths of nums1 and nums2. The space complexity is O(n + m) for storing the dynamic programming state transitions.

What Interviewers Usually Probe

  • Look for an understanding of dynamic programming concepts and efficient state transitions.
  • Expect the candidate to discuss the trade-off between path choices at common elements.
  • Be aware of the candidate's ability to handle edge cases, such as no common elements between the arrays.

Common Pitfalls or Variants

Common pitfalls

  • Failing to properly handle state transitions when encountering common elements.
  • Using brute-force methods without leveraging dynamic programming to optimize the solution.
  • Overcomplicating the problem by neglecting the efficient partitioning of the arrays based on common values.

Follow-up variants

  • Modify the problem to allow multiple common values between the arrays.
  • Change the problem to consider non-strictly increasing arrays.
  • Introduce a limit on the number of common elements to explore how the solution adapts.

How GhostInterview Helps

  • GhostInterview helps by guiding the candidate through the dynamic programming approach, ensuring they understand how to partition arrays efficiently.
  • The platform offers real-time hints on how to select the best path when encountering common values.
  • It assists in handling large arrays efficiently, giving the user tools to optimize space and time complexity during implementation.

Topic Pages

FAQ

What is the best approach to solve 'Get the Maximum Score'?

The optimal approach is to use dynamic programming combined with a two-pointer technique. Partition the arrays based on common elements and maximize the sum by selecting the best path at each state transition.

How can I handle large arrays in this problem?

You should aim for a solution with O(n + m) time and O(n + m) space complexity to efficiently handle arrays of length up to 100,000. Dynamic programming is key to optimizing performance.

What are common pitfalls when solving this problem?

Common pitfalls include failing to optimize state transitions, using brute-force approaches, and not handling edge cases such as arrays with no common elements.

How does dynamic programming work in this problem?

Dynamic programming stores intermediate results of path scores, allowing you to compute the maximum possible score at each stage and transition efficiently between paths.

What should I do when the arrays have no common elements?

If the arrays have no common elements, simply select the path from the array with the highest sum. This can be done using the two-pointer technique.

GhostInterview Solver

Need direct help with Get the Maximum Score instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Get the Maximum Score from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.