We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent f4d7ad6 commit cda892aCopy full SHA for cda892a
scripts/algorithms/M/Minimum Operations to Halve Array Sum/Minimum Operations to Halve Array Sum.js
@@ -1,28 +1,29 @@
1
+// Runtime: 1606 ms (Top 23.08%) | Memory: 137.1 MB (Top 30.77%)
2
var halveArray = function(nums) {
3
const n = nums.length;
4
const maxHeap = new MaxPriorityQueue({ priority: x => x });
-
5
+
6
let startSum = 0;
7
8
for (const num of nums) {
9
maxHeap.enqueue(num);
10
startSum += num;
11
}
12
13
let currSum = startSum;
14
15
let numberOfOperations = 0;
16
17
while (currSum > startSum / 2) {
18
const biggestNum = maxHeap.dequeue().element;
19
20
const halfNum = biggestNum / 2;
21
22
numberOfOperations += 1;
23
currSum -= halfNum;
24
25
maxHeap.enqueue(halfNum);
26
27
28
return numberOfOperations;
-};
29
+};
0 commit comments