1 Star 0 Fork 0

yuhang2__2/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
minimize-deviation-in-array.cpp 777 Bytes
一键复制 编辑 原始数据 按行查看 历史
// Time: O((n * log(max_num)) * logn)
// Space: O(n)
class Solution {
public:
int minimumDeviation(vector<int>& nums) {
vector<int> evens;
for (const auto& num : nums) {
evens.emplace_back(num % 2 ? 2 * num : num);
}
int min_elem = *min_element(cbegin(evens), cend(evens));
priority_queue<int> max_heap(less<int>(), move(evens));
int result = numeric_limits<int>::max();
while (size(max_heap) == size(nums)) {
const auto num = max_heap.top(); max_heap.pop();
result = min(result, num - min_elem);
if (num % 2 == 0) {
min_elem = min(min_elem, num / 2);
max_heap.emplace(num / 2);
}
}
return result;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/yuhang2__2/LeetCode-Solutions.git
git@gitee.com:yuhang2__2/LeetCode-Solutions.git
yuhang2__2
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助