FunctionRacerProblemsRace

Best Path Across Two Lists

HardTwo pointersPython, JavaScript, TypeScript, Java, C++

first and second are both sorted in strictly increasing order. A path starts at the first item of either list and walks to the right, to the end of the list it is on. Whenever it stands on a number that also appears in the other list, it may switch over to that number in the other list and walk on from there.

The score of a path is the sum of the numbers it visits; a number where the path switches is counted once. Return the largest score.

Example 1

first [1,4,20,30]
second [3,4,6,30,31]
returns 88

Start on the second list with 3, switch at 4 to pick up 20, and switch back at 30 to pick up 31: 3 + 4 + 20 + 30 + 31 = 88.

Example 2

first [1,2,3]
second [10]
returns 10

The lists share no number, so the path stays on one list. The second list alone gives 10.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Two pointers problems