Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Fruit Into Baskets
Bookmark
Brute Force
Sliding Window + Hash Map
Sliding Window
Input
Example 1
Example 2
Example 3
Longer
Custom
fruits
=
[1, 2, 1]
0
1
2
1
2
1
A:
empty
B:
empty
0
1
2
1
2
1
A:
empty
B:
empty
0
1
2
1
2
1
left
right
pick fruits[
0
] =
1
A:
empty
B:
empty
0
1
2
maxFruits = 1
1
2
1
left
right
windowSize
1
> maxFruits
0
maxFruits = 1
A:
1
B:
empty
0
1
2
maxFruits = 2
1
2
1
left
right
windowSize
2
> maxFruits
1
maxFruits = 2
A:
1
B:
2
0
1
2
maxFruits = 2
1
2
1
left
right
1
fits in a basket
A:
1
B:
2
0
1
2
maxFruits = 3
1
2
1
left
right
pick fruits[
1
] =
2
A:
empty
B:
empty
0
1
2
maxFruits = 3
1
2
1
left
right
windowSize
1
<= maxFruits
3
keep maxFruits
A:
2
B:
empty
0
1
2
maxFruits = 3
1
2
1
left
right
windowSize
2
<= maxFruits
3
keep maxFruits
A:
2
B:
1
0
1
2
maxFruits = 3
1
2
1
left
right
1
fits in a basket
A:
1
B:
empty
0
1
2
maxFruits = 3
1
2
1
maxFruits =
3
algo
master
.
io
Step:
Start of algorithm
0 / 20
Input
Example 1
Example 2
Example 3
Longer
Custom
fruits
=
[1, 2, 1]
0 / 20
algo
master
.
io
Step:
Start of algorithm