-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problem: No.2208 (#3537)
No.2208.Minimum Operations to Halve Array Sum
- Loading branch information
Showing
8 changed files
with
98 additions
and
194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 8 additions & 8 deletions
16
solution/2200-2299/2208.Minimum Operations to Halve Array Sum/Solution.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
class Solution { | ||
public: | ||
int halveArray(vector<int>& nums) { | ||
priority_queue<double> q; | ||
priority_queue<double> pq; | ||
double s = 0; | ||
for (int& v : nums) { | ||
s += v; | ||
q.push(v); | ||
for (int x : nums) { | ||
s += x; | ||
pq.push((double) x); | ||
} | ||
s /= 2.0; | ||
int ans = 0; | ||
while (s > 0) { | ||
double t = q.top() / 2; | ||
q.pop(); | ||
double t = pq.top() / 2.0; | ||
pq.pop(); | ||
s -= t; | ||
q.push(t); | ||
pq.push(t); | ||
++ans; | ||
} | ||
return ans; | ||
} | ||
}; | ||
}; |
Oops, something went wrong.