File tree Expand file tree Collapse file tree 2 files changed +40
-0
lines changed Expand file tree Collapse file tree 2 files changed +40
-0
lines changed Original file line number Diff line number Diff line change 2003
2003
2204|[ Distance to a Cycle in Undirected Graph] ( ./solutions/2204-distance-to-a-cycle-in-undirected-graph.js ) |Hard|
2004
2004
2206|[ Divide Array Into Equal Pairs] ( ./solutions/2206-divide-array-into-equal-pairs.js ) |Easy|
2005
2005
2207|[ Maximize Number of Subsequences in a String] ( ./solutions/2207-maximize-number-of-subsequences-in-a-string.js ) |Medium|
2006
+ 2208|[ Minimum Operations to Halve Array Sum] ( ./solutions/2208-minimum-operations-to-halve-array-sum.js ) |Medium|
2006
2007
2209|[ Minimum White Tiles After Covering With Carpets] ( ./solutions/2209-minimum-white-tiles-after-covering-with-carpets.js ) |Hard|
2007
2008
2210|[ Count Hills and Valleys in an Array] ( ./solutions/2210-count-hills-and-valleys-in-an-array.js ) |Easy|
2008
2009
2211|[ Count Collisions on a Road] ( ./solutions/2211-count-collisions-on-a-road.js ) |Medium|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 2208. Minimum Operations to Halve Array Sum
3
+ * https://leetcode.com/problems/minimum-operations-to-halve-array-sum/
4
+ * Difficulty: Medium
5
+ *
6
+ * You are given an array nums of positive integers. In one operation, you can choose any
7
+ * number from nums and reduce it to exactly half the number. (Note that you may choose
8
+ * this reduced number in future operations.)
9
+ *
10
+ * Return the minimum number of operations to reduce the sum of nums by at least half.
11
+ */
12
+
13
+ /**
14
+ * @param {number[] } nums
15
+ * @return {number }
16
+ */
17
+ var halveArray = function ( nums ) {
18
+ const pq = new PriorityQueue ( ( a , b ) => b - a ) ;
19
+ let totalSum = 0 ;
20
+
21
+ for ( const num of nums ) {
22
+ pq . enqueue ( num ) ;
23
+ totalSum += num ;
24
+ }
25
+
26
+ const target = totalSum / 2 ;
27
+ let reducedSum = 0 ;
28
+ let result = 0 ;
29
+
30
+ while ( reducedSum < target ) {
31
+ const largest = pq . dequeue ( ) ;
32
+ const halved = largest / 2 ;
33
+ reducedSum += halved ;
34
+ pq . enqueue ( halved ) ;
35
+ result ++ ;
36
+ }
37
+
38
+ return result ;
39
+ } ;
You can’t perform that action at this time.
0 commit comments