medium
Fruit Into Baskets
Trees are planted in a row and each produces one fruit type, given as an integer array. Starting at any tree and moving right, you pick one fruit per tree, but you carry only two baskets and each basket holds a single type. Return the maximum number of fruits you can collect.
Constraints
- 1 ≤ n ≤ 10^5
- 0 ≤ fruits[i] < n
Examples
in: fruits = [1,2,3,2,2]
out: 4
Pick from trees 2..5: types 2 and 3 only.
Code it yourself
Solve in
Test execution is not yet available for this exercise.Practice journal →Draft saved in this browser.
Hints:
Which approach applies?
Choose an approach to check your pattern recognition, or reveal the discussion when you need help.