We’re preparing your current view and syncing the latest data.
You are visiting a row of fruit trees represented by an integer array fruits where fruits[i] is the type of fruit on the i-th tree. You have two baskets and want to collect the maximum number of fruits in them with the restriction that each basket can only hold one type of fruit. Starting from any tree, you must pick exactly one fruit from every tree (including the start tree) while moving to the right, until you cannot pick more fruits according to the baskets' constraints. Return the maximum number of fruits you can pick.
An integer array fruits representing the types of fruits on each tree in a row.
An integer representing the maximum number of fruits that can be collected with two baskets.
1 <= fruits.length <= 10^5 0 <= fruits[i] < fruits.length
Example 1
Input
[1,2,1]
Output
3
Explanation
Take fruits from all three trees because there are only two types of fruits.
Example 2
Input
[0,1,2,2]
Output
3
Explanation
The longest window with at most two types is [1,2,2].
Example 3
Input
[1,2,3,2,2]
Output
4
Explanation
The longest window with at most two types is [2,3,2,2].