Fruit Into Baskets
Problem Trees in a row each bear one fruit type (fruits[i]). With two baskets, each holding only one type, start at any tree and move right picking one fruit per tree; stop when a third type appears. Maximize fruits picked — i.e. find the longest subarray with at most 2 distinct values.
Input / Output
- Input: int array fruits. Output: max window length.
Constraints
- n up to 10^5; O(n) expected.
Example
- [1,2,3,2,2] → 4 ([2,3,2,2]); [3,3,3,1,2,1,1,2,3,3,4] → 5.
asked …