題目:
https://leetcode.com/problems/minimum-operations-to-halve-array-sum/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| class Solution { public: int halveArray(vector<int>& nums) { priority_queue<double> heap; double sum = 0; for(int i=0; i<nums.size(); ++i){ sum += nums.at(i); heap.push((double)nums.at(i)); } sum /= 2.0; double total = 0; int counts = 0; while(total < sum){ ++counts; double maxi = heap.top(); heap.pop(); maxi /= 2.0; total += maxi; heap.push(maxi); } return counts; } };
|