Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Boats to Save People
Bookmark
Brute Force
Two Pointers
Input
Example 1
Example 2
No pairs
Custom
people
=
[1, 2, 2, 3]
,
limit
=
3
0
1
2
3
1
2
2
3
limit =
3
boats =
0
0
1
2
3
1
2
2
3
limit =
3
boats =
0
0
1
2
3
1
2
2
3
heavy
limit =
3
boats =
0
scan all n for the heaviest unused →
people[3] = 3
0
1
2
3
1
2
2
3
heavy
boats
3
limit =
3
boats =
1
boat 1:
3
boards, now look for a partner
0
1
2
3
1
2
2
3
light
heavy
boats
3
limit =
3
boats =
1
3
+
1
=
4
>
3
→ the light person waits
0
1
2
3
1
2
2
3
heavy
boats
3
limit =
3
boats =
1
scan all n for the heaviest unused →
people[1] = 2
0
1
2
3
1
2
2
3
light
heavy
boats
3
2
limit =
3
boats =
2
scan all n for the lightest unused →
people[0] = 1
0
1
2
3
1
2
2
3
light
heavy
boats
3
1 + 2
limit =
3
boats =
2
2
+
1
=
3
≤
3
→ both fit
0
1
2
3
1
2
2
3
heavy
boats
3
1 + 2
2
limit =
3
boats =
3
boat 3:
2
boards, now look for a partner
0
1
2
3
1
2
2
3
heavy
boats
3
1 + 2
2
limit =
3
boats =
3
scan all n for the lightest unused →
nobody left
0
1
2
3
1
2
2
3
boats
3
1 + 2
2
limit =
3
boats =
3
return 3
4 people rescued
algo
master
.
io
Step:
Save 4 people with boats that hold 2 and carry at most 3
0 / 14
Input
Example 1
Example 2
No pairs
Custom
people
=
[1, 2, 2, 3]
,
limit
=
3
0 / 14
algo
master
.
io
Step:
Save 4 people with boats that hold 2 and carry at most 3