FunctionRacerProblemsRace

Fruit Into Baskets

MediumTwo pointersPython, JavaScript, TypeScript, Java, C++

The list fruits gives the type fruits[i] of the fruit on tree i, along a row. You walk along the row picking one fruit from each tree, starting anywhere and never skipping a tree. You have two baskets and each can hold only one type of fruit.

Stop when a tree has a third type you cannot carry. Return the most fruit you can pick.

Example 1

fruits [1,2,1]
returns 3

Only two types, take all 3.

Example 2

fruits [0,1,2,2]
returns 3

Start at index 1: types 1 and 2 give 3 fruits.

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