Skip to content

Commit 16da511

Browse files
committed
Add solution #2208
1 parent ba18ec5 commit 16da511

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2003,6 +2003,7 @@
20032003
2204|[Distance to a Cycle in Undirected Graph](./solutions/2204-distance-to-a-cycle-in-undirected-graph.js)|Hard|
20042004
2206|[Divide Array Into Equal Pairs](./solutions/2206-divide-array-into-equal-pairs.js)|Easy|
20052005
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|
20062007
2209|[Minimum White Tiles After Covering With Carpets](./solutions/2209-minimum-white-tiles-after-covering-with-carpets.js)|Hard|
20072008
2210|[Count Hills and Valleys in an Array](./solutions/2210-count-hills-and-valleys-in-an-array.js)|Easy|
20082009
2211|[Count Collisions on a Road](./solutions/2211-count-collisions-on-a-road.js)|Medium|
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy