Practice Fruit Into Baskets
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
Start at any position in fruits and move right, collecting exactly one fruit from every visited tree. Two baskets are available; each holds an unlimited amount of only one fruit type. Stop before the first tree whose fruit cannot fit those baskets. Return the maximum fruits obtainable, equivalently the longest contiguous segment with at most two distinct types.
Source: Fruit Into Baskets on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
fruits = [1,2,1]
3
# Example 2
fruits = [0,1,2,2]
3
# Example 3
fruits = [1,2,3,2,2]
4
Constraints
Related LeetCode Problems
Practice more problems similar to Fruit Into Baskets.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.