Learn
Practice
Newsletter
Resources
Mobile
New
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Russian Doll Envelopes
Bookmark
Input
Example 1
Example 2
Example 3
Custom
envelopes
=
[[5,4],[6,4],[6,7],[2,3]]
size =
0
w x h
5
×
4
6
×
4
6
×
7
2
×
3
0
1
2
3
tails
·
·
·
·
most envelopes that fit one strictly inside the next?
size =
0
w x h
5
×
4
6
×
4
6
×
7
2
×
3
0
1
2
3
tails
·
·
·
·
most envelopes that fit one strictly inside the next?
size =
0
w x h
2
×
3
5
×
4
6
×
7
6
×
4
0
1
2
3
h=3
tails
3
·
·
·
tails[0]
=
3
extends the longest chain
size =
1
w x h
2
×
3
5
×
4
6
×
7
6
×
4
0
1
2
3
tails
3
·
·
·
left 0
right 1
5×4
: place height
4
into tails (first tail ≥ 4)
size =
1
w x h
2
×
3
5
×
4
6
×
7
6
×
4
0
1
2
3
tails
3
·
·
·
left 1
right 1
search while
left 1
<
right 1
size =
2
w x h
2
×
3
5
×
4
6
×
7
6
×
4
0
1
2
3
tails
3
4
·
·
left 0
right 2
6×7
: place height
7
into tails (first tail ≥ 7)
size =
2
w x h
2
×
3
5
×
4
6
×
7
6
×
4
0
1
2
3
tails
3
4
·
·
left 2
right 2
4
<
7
: go right, left =
2
size =
3
w x h
2
×
3
5
×
4
6
×
7
6
×
4
0
1
2
3
tails
3
4
7
·
appended at the end →
size = 3
size =
3
w x h
2
×
3
5
×
4
6
×
7
6
×
4
0
1
2
3
tails
3
4
7
·
left 0
right 1
4
≥
4
: go left, right =
1
size =
3
w x h
2
×
3
5
×
4
6
×
7
6
×
4
0
1
2
3
tails
3
4
7
·
left 1
right 1
3
<
4
: go right, left =
1
size =
3
w x h
2
×
3
5
×
4
6
×
7
6
×
4
6×7
5×4
2×3
3
nest:
2×3 → 5×4 → 6×7
algo
master
.
io
Step:
Nest envelopes: each must fit strictly inside the next (both width AND height larger)
0 / 33
Input
Example 1
Example 2
Example 3
Custom
envelopes
=
[[5,4],[6,4],[6,7],[2,3]]
0 / 33
algo
master
.
io
Step:
Nest envelopes: each must fit strictly inside the next (both width AND height larger)