-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimumDeviation.cpp
More file actions
55 lines (53 loc) · 1.64 KB
/
minimumDeviation.cpp
File metadata and controls
55 lines (53 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// O(nlog(a_max)logn)
class Solution {
public:
int minimumDeviation(vector<int>& nums) {
priority_queue<int> pq;
int mi = 1e9 + 5;
for (int &e: nums) {
if (e & 1) e <<= 1;
pq.push(e);
mi = std::min(mi, e);
}
int ans = pq.top() - mi;
while ((pq.top() & 1) == 0) {
int e = pq.top();
pq.pop();
ans = std::min(ans, e - mi);
e >>= 1;
mi = std::min(mi, e);
pq.push(e);
}
return std::min(pq.top() - mi, ans);
}
};
// O(nlogn)
class Solution {
public:
int minimumDeviation(vector<int>& nums) {
int p_max = 1;
for(int a : nums) p_max = max(p_max, a >> (__builtin_ctz(a)));
vector<int> upper;
int min = p_max;
for(int a : nums){
if(a & 1) a <<= 1;
if(a >= p_max){
a >>= __builtin_clz(p_max) - __builtin_clz(a);
if(a < p_max) a <<= 1;
upper.push_back(a);
}
min = std::min(min, a);
}
sort(upper.begin(), upper.end());
int ans = upper.back() - min;
for(int i = upper.size() - 1; upper[i] > p_max; i -= 1){
min = std::min(min, upper[i] >> 1);
ans = std::min(ans, upper[i - 1] - min);
}
return ans;
}
};
// 作者:何逊
// 链接:https://leetcode.cn/problems/minimize-deviation-in-array/solutions/503280/yi-chong-fu-za-du-geng-di-de-zuo-fa-by-heltion-2/
// 来源:力扣(LeetCode)
// 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。