#3066
Medium Algorithms Minimum operations to exceed threshold value ii
Array Heap (Priority Queue) Simulation
45.8% acceptance
Feb 25, 2026
631
70
You are given a 0-indexed integer array nums, and an integer k.
You are allowed to perform some operations on nums, where in a single operation, you can:
Select the two smallest integers x and y from nums.
Remove x and y from nums.
Insert (min(x, y) * 2 + max(x, y)) at any position in the array.
Return the minimum number of operations needed so that all elements of the array are greater than or equal to k.
Solution
Rust
Time O(n log n)
Space O(1)
impl Solution {
pub fn min_operations(nums: Vec<i32>, k: i32) -> i32 {
use std::collections::BinaryHeap;
use std::cmp::Reverse;
let mut heap: BinaryHeap<Reverse<i64>> = nums.iter().map(|&x| Reverse(x as i64)).collect();
let mut ops = 0;
while heap.peek().map(|Reverse(x)| *x) < Some(k as i64) {
let Reverse(x) = heap.pop().unwrap();
let Reverse(y) = heap.pop().unwrap();
heap.push(Reverse(x * 2 + y));
ops += 1;
}
ops
}
}