forked from kamyu104/LintCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maximum-gap.cpp
47 lines (42 loc) · 1.56 KB
/
maximum-gap.cpp
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
// Time: O(n)
// Space: O(n)
class Solution {
public:
/**
* @param nums: a vector of integers
* @return: the maximum difference
*/
int maximumGap(vector<int> nums) {
if (nums.size() < 2) {
return 0;
}
// Init bucket.
int max_val = *max_element(nums.cbegin(), nums.cend());
int min_val = *min_element(nums.cbegin(), nums.cend());
int gap = max(1, static_cast<int>((max_val - min_val) /
(nums.size() - 1)));
map<int, array<int, 2>> bucket;
typedef enum {MIN, MAX} ValueType;
// Find the bucket where the n should be put.
for (const auto& n : nums) {
// min_val / max_val is in the first / last bucket.
if (n == max_val || n == min_val) {
continue ;
}
int i = (n - min_val) / gap;
bucket[i][MIN] = min(!bucket[i][MIN] ? INT_MAX :
bucket[i][MIN], n);
bucket[i][MAX] = max(!bucket[i][MAX] ? INT_MIN :
bucket[i][MAX], n);
}
// Count each bucket gap between the first and the last bucket.
int max_gap = 0, pre_bucket_max = min_val;
for (auto& kvp : bucket) {
max_gap = max(max_gap, kvp.second[MIN] - pre_bucket_max);
pre_bucket_max = (kvp.second)[MAX];
}
// Count the last bucket.
max_gap = max(max_gap, max_val - pre_bucket_max);
return max_gap;
}
};