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.
Input: sticks =[2,4,3]Output: 14Explanation: You start with sticks =[2,4,3].1. Combine sticks 2 and 3for a cost of 2+3=5. Now you have sticks =[5,4].2. Combine sticks 5 and 4for a cost of 5+4=9. Now you have sticks =[9].There is only one stick left, so you are done. The total cost is5+9=14.
Example 2:
1
2
3
4
5
6
7
Input: sticks =[1,8,3,5]Output: 30Explanation: You start with sticks =[1,8,3,5].1. Combine sticks 1 and 3for a cost of 1+3=4. Now you have sticks =[4,8,5].2. Combine sticks 4 and 5for a cost of 4+5=9. Now you have sticks =[9,8].3. Combine sticks 9 and 8for a cost of 9+8=17. Now you have sticks =[17].There is only one stick left, so you are done. The total cost is4+9+17=30.
Example 3:
1
2
3
Input: sticks =[5]Output: 0Explanation: There is only one stick, so you don't need to do anything. The total cost is0.
To minimize the total cost, always connect the two smallest sticks first. This is because connecting larger sticks later will accumulate less cost overall. Using a min-heap allows us to efficiently pick the two smallest sticks at each step.
classSolution {
public:int connectSticks(vector<int>& sticks) {
priority_queue<int, vector<int>, greater<int>> pq(sticks.begin(), sticks.end());
int ans =0;
while (pq.size() >1) {
int a = pq.top(); pq.pop();
int b = pq.top(); pq.pop();
ans += a + b;
pq.push(a + b);
}
return ans;
}
};
classSolution {
publicintconnectSticks(int[] sticks) {
PriorityQueue<Integer> pq =new PriorityQueue<>();
for (int v : sticks) pq.offer(v);
int ans = 0;
while (pq.size() > 1) {
int a = pq.poll(), b = pq.poll();
ans += a + b;
pq.offer(a + b);
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution {
funconnectSticks(sticks: IntArray): Int {
val pq = PriorityQueue<Int>()
sticks.forEach { pq.add(it) }
var ans = 0while (pq.size > 1) {
val a = pq.poll()
val b = pq.poll()
ans += a + b
pq.add(a + b)
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
classSolution:
defconnectSticks(self, sticks: list[int]) -> int:
import heapq
heapq.heapify(sticks)
ans =0while len(sticks) >1:
a = heapq.heappop(sticks)
b = heapq.heappop(sticks)
ans += a + b
heapq.heappush(sticks, a + b)
return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
impl Solution {
pubfnconnect_sticks(sticks: Vec<i32>) -> i32 {
use std::collections::BinaryHeap;
letmut heap = BinaryHeap::new();
for v in sticks { heap.push(-v); }
letmut ans =0;
while heap.len() >1 {
let a =-heap.pop().unwrap();
let b =-heap.pop().unwrap();
ans += a + b;
heap.push(-(a + b));
}
ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution {
connectSticks(sticks: number[]):number {
constpq= [...sticks].sort((a, b) =>a-b);
letans=0;
while (pq.length>1) {
pq.sort((a, b) =>a-b);
consta=pq.shift()!;
constb=pq.shift()!;
ans+=a+b;
pq.push(a+b);
}
returnans;
}
}