LeetCode 1167. Minimum Cost to Connect Sticks

Problem Statement


use std::collections::BinaryHeap;

impl Solution {
pub fn connect_sticks(sticks: Vec<i32>) -> i32 {
let (mut heap, mut ans) = (BinaryHeap::new(), 0);
for stick in sticks {
heap.push(stick * -1);
}
while heap.len() > 1 {
let (a, b) = (heap.pop().unwrap() * -1, heap.pop().unwrap() * -1);
ans += a + b;
heap.push((a + b) * -1);
}
return ans;
}
}