FunctionRacerProblemsRace

Most Fruit Within Reach

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

Fruit trees stand along a straight path. Each item of trees is [position, fruit], the positions are all different and given in increasing order. You begin at position start and take at most steps steps; a step moves you one position left or right, and you may turn around whenever you like.

You pick all the fruit of every tree you stand at, including one at start. Return the most fruit you can pick.

Example 1

trees [[2,5],[6,3],[9,8]]
start 5
steps 5
returns 11

Walk right past 6 to 9: four steps and 3 + 8 = 11 fruit. Fetching the tree at 2 as well would take more than five steps.

Example 2

trees [[0,9]]
start 4
steps 3
returns 0

The only tree is four steps away.

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