You walk along a row of trees given as fruits, where fruits[i] is the type of fruit on tree i. You carry two baskets and each basket can hold only one type of fruit (any amount of it). You pick exactly one fruit from every tree you pass, and you must stop when you reach a tree whose fruit fits in neither basket.
Return the maximum number of fruits you can collect — that is, the length of the longest contiguous subarray containing at most 2 distinct values.
- You may start at any tree
- An empty row yields
0 - Target complexity: O(n)