Skip to content

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.

Medium#904
LeetCode Problem

Fruit Into Baskets

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

Input
fruits = [1,2,1]
Output
3

# Example 2

Input
fruits = [0,1,2,2]
Output
3

# Example 3

Input
fruits = [1,2,3,2,2]
Output
4

Constraints

1 <= fruits.length <= 10^5
0 <= fruits[i] < fruits.length

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.