forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_2208.java
More file actions
27 lines (25 loc) · 762 Bytes
/
_2208.java
File metadata and controls
27 lines (25 loc) · 762 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.fishercoder.solutions;
import java.util.PriorityQueue;
public class _2208 {
public static class Solution1 {
public int halveArray(int[] nums) {
PriorityQueue<Double> heap = new PriorityQueue<>((a, b) -> Double.compare(b, a));
double sum = 0;
for (int num : nums) {
heap.offer(Double.valueOf(num));
sum += num;
}
double half = (double) sum / 2;
int ops = 0;
while (sum > half) {
Double max = heap.poll();
sum -= max;
double h = max / 2;
sum += h;
heap.offer(h);
ops++;
}
return ops;
}
}
}