美文网首页
LeetCode 164. Maximum Gap

LeetCode 164. Maximum Gap

作者: 柳正来 | 来源:发表于2017-04-08 22:21 被阅读320次

Description

Given an unsorted array, find the maximum difference between the successive elements in its sorted form.

Try to solve it in linear time/space.

Return 0 if the array contains less than 2 elements.

You may assume all elements in the array are non-negative integers and fit in the 32-bit signed integer range.

Explanation

See here

Solution 1 - Sort

// OJ: https://leetcode.com/problems/maximum-gap
// Auther: github.com/lzl124631x
// Time: O(NlogN)
// Space: O(1)
class Solution {
public:
  int maximumGap(vector<int>& nums) {
    sort(nums.begin(), nums.end());
    int ans = 0;
    for (int i = 1; i < nums.size(); ++i) ans = max(ans, nums[i] - nums[i - 1]);
    return ans;
  }
};

Solution 2 - Radix Sort

// OJ: https://leetcode.com/problems/maximum-gap
// Auther: github.com/lzl124631x
// Time: O(N)
// Space: O(N)
// Ref: https://leetcode.com/articles/maximum-gap
class Solution {
public:
  int maximumGap(vector<int>& nums) {
    if (nums.size() < 2) return 0;
    int maxVal = *max_element(nums.begin(), nums.end());
    vector<int> aux(nums.size());
    for (int exp = 1, radix = 10; maxVal / exp > 0; exp *= radix) {
      vector<int> cnt(radix, 0);
      for (int n : nums) cnt[(n / exp) % radix]++;
      for (int i = 1; i < cnt.size(); ++i) cnt[i] += cnt[i - 1];
      for (int i = nums.size() - 1; i >= 0; --i) aux[--cnt[(nums[i] / exp) % radix]] = nums[i];
      nums = aux;
    }
    int ans = 0;
    for (int i = 1; i < nums.size(); ++i) ans = max(ans, nums[i] - nums[i - 1]);
    return ans;
  }
};

Solution 3 - Buckets and The Pigeonhole Principle

// OJ: https://leetcode.com/problems/maximum-gap
// Auther: github.com/lzl124631x
// Time: O(N)
// Space: O(N)
// Ref: https://discuss.leetcode.com/topic/5999/bucket-sort-java-solution-with-explanation-o-n-time-and-space
class Solution {
public:
  int maximumGap(vector<int>& nums) {
    if (nums.size() < 2) return 0;
    int N = nums.size(), minVal = INT_MAX, maxVal = INT_MIN;
    for (int n : nums) {
      minVal = min(minVal, n);
      maxVal = max(maxVal, n);
    }
    int gap = ceil((double)(maxVal - minVal) / (N - 1));
    vector<int> mins(N - 1, INT_MAX), maxes(N - 1, INT_MIN);
    for (int n : nums) {
      if (n == minVal || n == maxVal) continue;
      int i = (n - minVal) / gap;
      mins[i] = min(mins[i], n);
      maxes[i] = max(maxes[i], n);
    }
    int ans = INT_MIN, prev = minVal;
    for (int i = 0; i < N - 1; ++i) {
      if (mins[i] == INT_MAX) continue;
      ans = max(ans, mins[i] - prev);
      prev = maxes[i];
    }
    return max(ans, maxVal - prev);
  }
};

相关文章

  • 2019-01-16

    LeetCode 164. Maximum Gap Description Given an unsorted a...

  • Leetcode 164. Maximum Gap

    Given an unsorted array, find the maximum difference betw...

  • LeetCode 164. Maximum Gap

    Description Given an unsorted array, find the maximum dif...

  • ARTS 第20周

    ARTS 第20周分享 [TOC] Algorithm 164. Maximum Gap [hard] [题目描述...

  • 164. Maximum Gap

    问题描述 Given an unsorted array, find the maximum difference...

  • Leetcode - Maximum Gap

    My code: 看到这道题目,第一个想法是排序。然后要求排序时间复杂度是 O(n)就想到了 radix sort...

  • Maximum Gap

    https://leetcode.com/problems/maximum-gap/给定一个无序int数组,求出排...

  • 164. Maximum Gap(最大间距)

    题目 LeetCode中文站 解答 根据题意我们就可以直到这是一题先排序,然后再寻找最大间距的题目,我们先使用最简...

  • Leetcode-164-Maximum Gap

    给定一个未排序的数组,求排序后数组中相邻元素之差的最大值。 这题最大的思维盲点就在于的复杂度让人直接放弃包含排序的...

  • 4.5 leet 53|70|83

    Maximum Subarrayhttps://leetcode.com/problems/maximum-sub...

网友评论

      本文标题:LeetCode 164. Maximum Gap

      本文链接:https://www.haomeiwen.com/subject/rsdlattx.html