Fruit Into Baskets
Trees stand in a row, and fruits[i] is the kind of fruit growing on tree i. You carry two baskets, and each basket holds only one kind of fruit (but any amount of it).
You pick a starting tree and then walk to the right, picking exactly one fruit from every tree you pass. You must stop at the first tree whose fruit fits in neither basket. You cannot skip trees.
Return the largest number of fruits you can pick. Equivalently: the length of the longest contiguous stretch of trees that contains at most two different kinds of fruit.
Constraints
1 <= fruits.length <= 10^50 <= fruits[i] < fruits.length
Examples
Example 1
Input: fruits = [1,2,1] Output: 3
Only two kinds of fruit exist, so all three trees can be picked.
Example 2
Input: fruits = [0,1,2,2] Output: 3
Starting at the 1 you can take 1, 2, 2: three trees. Starting at the 0 you must stop after 0, 1.
Example 3
Input: fruits = [1,2,3,2,2] Output: 4
Start at the first 2: 2, 3, 2, 2 uses two types and is 4 trees long.
JavaScript you'll use: Map and Set Arrays: create, read, update
/**
* @param {number[]} fruits
* @return {number}
*/
function totalFruit(fruits) {
}
Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.