1.题目解析
题目来源
2208.将数组和减半的最小操作数——力扣
测试用例
2.算法原理(贪心策略)
3.实战代码
class Solution {
public:
int halveArray(vector<int>& nums)
{
priority_queue<double> hash;
double sum = 0.0;
for(auto e : nums)
{
hash.push(e);
sum += e;
}
sum /= 2.0;
int count = 0;
while(sum > 0)
{
double top = hash.top() / 2.0;
hash.pop();
sum -= top;
count++;
hash.push(top);
}
return count;
}
};
代码解析