Untitled
import java.util.PriorityQueue; public class Solution { public int minOperations(int[] nums, int k) { // Create a min-heap PriorityQueue<Integer> minHeap = new PriorityQueue<>(); for (int num : nums) { minHeap.add(num); } int operations = 0; while (minHeap.peek() < k) { // Extract the two smallest numbers int x = minHeap.poll(); int y = minHeap.poll(); // Perform the operation int newValue = Math.min(x, y) * 2 + Math.max(x, y); // Insert the new value into the heap minHeap.add(newValue); // Increment operation count operations++; } return operations; } }
Leave a Comment