1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| class Solution { public int halveArray(int[] nums) { double sum = 0; Queue<Double> maxHeap = new PriorityQueue<>((o1, o2) -> Double.compare(o2, o1)); for (int num : nums) { sum += num; maxHeap.offer((double) num); } double halfSum = sum / 2.0;
int operations = 0; while (sum > halfSum) { Double maxNum = maxHeap.poll(); maxHeap.offer(maxNum / 2); sum -= maxNum / 2; operations++; } return operations; } }
|
References
2208. Minimum Operations to Halve Array Sum