-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy path1167-minimum-cost-to-connect-sticks.js
More file actions
38 lines (33 loc) · 1.09 KB
/
1167-minimum-cost-to-connect-sticks.js
File metadata and controls
38 lines (33 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* 1167. Minimum Cost to Connect Sticks
* https://leetcode.com/problems/minimum-cost-to-connect-sticks/
* Difficulty: Medium
*
* You have some number of sticks with positive integer lengths. These lengths are given as
* an array sticks, where sticks[i] is the length of the ith stick.
*
* You can connect any two sticks of lengths x and y into one stick by paying a cost of x + y.
* You must connect all the sticks until there is only one stick remaining.
*
* Return the minimum cost of connecting all the given sticks into one stick in this way.
*/
/**
* @param {number[]} sticks
* @return {number}
*/
var connectSticks = function(sticks) {
const minHeap = [...sticks].sort((a, b) => a - b);
let result = 0;
while (minHeap.length > 1) {
const first = minHeap.shift();
const second = minHeap.shift();
const combinedCost = first + second;
result += combinedCost;
let insertIndex = 0;
while (insertIndex < minHeap.length && minHeap[insertIndex] < combinedCost) {
insertIndex++;
}
minHeap.splice(insertIndex, 0, combinedCost);
}
return result;
};