代码拉取完成,页面将自动刷新
// Time: O(n)
// Space: O(n)
class Solution {
public:
struct Bucket {
int max = numeric_limits<int>::min();
int min = numeric_limits<int>::max();
};
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)));
vector<Bucket> buckets((max_val - min_val) / gap + 1);
// 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;
buckets[i].min = min(buckets[i].min, n);
buckets[i].max = max(buckets[i].max, n);
}
// Maximum gap should not be smaller than any gap inside the bucket.
// i.e. max_gap >= (max_val - min_val) / (count - 1)
// Thus, only count each bucket gap between the first and the last bucket.
int max_gap = 0, pre_bucket_max = min_val;
for (const auto& bucket : buckets) {
if (bucket.min != numeric_limits<int>::max()) {
max_gap = max(max_gap, bucket.min - pre_bucket_max);
pre_bucket_max = bucket.max;
}
}
// Count the last bucket.
max_gap = max(max_gap, max_val - pre_bucket_max);
return max_gap;
}
};
// Time: O(nlogn)
// Space: O(n)
class Solution2 {
public:
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;
using ValueType = enum {MIN, MAX};
// 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] ? numeric_limits<int>::max() :
bucket[i][MIN], n);
bucket[i][MAX] = max(!bucket[i][MAX] ? numeric_limits<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;
}
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。