Description

You have two baskets, each can hold only one fruit type. Collect maximum fruit from contiguous trees starting anywhere using at most 2 fruit types.

Examples

Input:fruits = [1,2,1]
Output:3
Explanation:

Can pick all 3 fruits with 2 types.

Input:fruits = [3,3,3,1,2,1,1,2,3,3,4]
Output:5
Explanation:

The longest contiguous sequence with at most 2 fruit types is [1,2,1,1,2] (indices 3-7), which has length 5. Although there are longer sequences like [3,3,3] at the start, it is possible to get more fruits by choosing the window with types 1 and 2.

Input:fruits = [1,1,1,1,1]
Output:5
Explanation:

All fruits are the same type (1), so it is possible to collect all 5 fruits using just one basket. Since only need 1 fruit type (which is ≤ 2), the entire array is valid.

Constraints

  • 1 ≤ fruits.length ≤ 10⁵

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!