#326 · Sliding window · Intermediate · 75 XP · about 15 min
An orchard has one long row of fruit trees, and trees[i] names the kind of fruit on tree i, such as 'apple'. You carry two baskets. Each basket can hold only one kind of fruit, but as much of it as you like.
You choose any tree to start at, then walk to the right, picking exactly one fruit from every tree you pass, the tree you start at included. You must stop when you reach a tree whose fruit fits in neither basket, or the end of the row. Write most_fruit(trees) that returns the largest number of fruits you can pick.
most_fruit(['apple', 'pear', 'apple']) # returns 3 most_fruit(['plum', 'apple', 'pear', 'pear', 'apple', 'fig']) # returns 4 most_fruit(['fig', 'fig', 'fig']) # returns 3
In the second example the best start is the second tree: apple, pear, pear and apple fit in two baskets, and the fig stops you.
0 <= len(trees) <= 100,000First solves only, ranked by stars and then time.