Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Minimum Cost to Connect Sticks
Bookmark
Sort and Merge
Min-Heap
Input
Example 1
Example 2
Single Stick
Custom
sticks
=
[2, 4, 3]
2
4
3
connecting two sticks costs their sum, connect the shortest first
2
4
3
connecting two sticks costs their sum, connect the shortest first
total cost =
0
sticks left =
3
round =
1
first
second
2
4
3
total cost =
0
sticks left =
3
round =
1
first
second
2
4
3
first = shift front =
2
total cost =
0
sticks left =
3
round =
1
first
second
2
4
3
second = shift front =
3
total cost =
5
sticks left =
2
round =
1
first
second
4
5
push the merged stick
5
back into the heap
total cost =
5
sticks left =
2
round =
2
first
second
4
5
total cost =
5
sticks left =
2
round =
2
first
second
4
5
first = shift front =
4
total cost =
5
sticks left =
2
round =
2
first
second
4
5
second = pop min =
5
total cost =
14
sticks left =
1
round =
2
first
second
9
push the merged stick
9
back into the heap
total cost =
14
9
Minimum total cost = 14
algo
master
.
io
Step:
totalCost = 0
Variable
Value
sticks
[2,4,3]
first
-
second
-
Variable
Value
totalCost
0
sticksLeft
3
0 / 13
Input
Example 1
Example 2
Single Stick
Custom
sticks
=
[2, 4, 3]
0 / 13
algo
master
.
io
Step:
totalCost = 0
Visualization
Variables