You are visiting a farm that has a single row of fruit trees arranged from left to right. The trees are represented by an integer array fruits where fruits[i] is the type of fruit the i-th tree produces. You want to collect as much fruit as possible. However, the owner has some strict rules: - You only have two baskets, and each basket can only hold a single type of fruit. There is no limit on the amount of fruit each basket can hold. - Starting from any tree of your choice, you must pick exactly one fruit from every tree (including the start tree) while moving to the right. The picked fruits must fit in one of your baskets. - Once you reach a tree with fruit that cannot fit in your baskets, you must stop. Given the integer array fruits, return the maximum number of fruits you can pick.
Example 1
Input: fruits = [3,3,3,1,2,1,1,2,3,3,4]
Output: 5
Explanation: The longest run with only two fruit types is [1,2,1,1,2] (types 1 and 2), which has 5 fruits. Any longer run would include a 3 or a 4 as a third type.
Example 2
Input: fruits = [1,2,1]
Output: 3
Explanation: The whole row has only types 1 and 2, so all 3 fruits fit in the two baskets.
Example 3
Input: fruits = [0,1,2,2]
Output: 3
Explanation: Start at the second tree and pick [1,2,2]. Starting at the first tree would bring in a third type, 2, after 0 and 1.
The brute force re-explores the same suffix from many starts. Instead, slide a window over the array — extend the right edge by adding fruits, and only shrink the left edge when the window has too many distinct types. The window's size at any moment is a valid answer; track the largest.
Run checks all cases above. Submit evaluates all test cases.