Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Last Stone Weight
Bookmark
Sorting
Max-Heap
Input
Example 1
Single Stone
Pairs Cancel
Custom
stones
=
[2, 7, 4, 1, 8, 1]
2
7
4
1
8
1
smash the two heaviest stones until at most one remains
2
7
4
1
8
1
smash the two heaviest stones until at most one remains
stones left =
6
round =
1
y
x
2
7
4
1
8
1
y = pop last =
8
stones left =
5
round =
1
y
x
2
4
1
1
1
8
-
7
=
1
→ push back
stones left =
5
round =
2
y
x
2
4
1
1
1
y = pop last =
4
stones left =
4
round =
2
y
x
1
1
1
2
4
-
2
=
2
→ push back
stones left =
4
y
x
1
1
1
2
sort
ascending
→ heaviest stone at the end
stones left =
4
round =
3
y
x
1
1
1
2
x = pop last =
1
stones left =
3
y
x
1
1
1
sort
ascending
→ heaviest stone at the end
stones left =
3
round =
4
y
x
1
1
1
x = pop last =
1
stones left =
1
1
Last stone weight = 1
algo
master
.
io
Step:
Start: stones = [2, 7, 4, 1, 8, 1]
Variable
Value
stones
Array(6) [2, 7, 4...]
y
-
x
-
Variable
Value
result
-
stonesLeft
6
0 / 17
Input
Example 1
Single Stone
Pairs Cancel
Custom
stones
=
[2, 7, 4, 1, 8, 1]
0 / 17
algo
master
.
io
Step:
Start: stones = [2, 7, 4, 1, 8, 1]
Visualization
Variables