Gamified challenge · Sliding window
trees[i] is the type of fruit on tree i in an orchard row. You carry two baskets, and each basket holds only one type of fruit (as much of it as you like). Start at any tree and walk right, picking exactly one fruit from every tree you pass. You must stop at the first tree whose fruit fits in neither basket.
Write totalFruit(trees) (total_fruit in Python) that returns the most fruit you can pick: the length of the longest run of consecutive trees with at most two fruit types. An empty row gives 0.
For [3, 3, 3, 1, 2, 1, 1, 2, 3, 3, 4] the best run is [1, 2, 1, 1, 2]: 5 fruit.
This game is part of the paid plans. Sign in, then pick a plan to write your answer, watch it play out, and step through the algorithm with its guide.
Sign in with Google