12ms C++ Suggested Solution


  • 17

    This problem has a naive solution using sort and linear scan. The suggested solution uses the idea of bucket sort. The following is a C++ implementation of the suggested solution.

    Suppose all the n elements in nums fall within [l, u], the maximum gap will not be smaller than gap = (u - l) / (n - 1). However, this gap may become 0 and so we take the maximum of it with 1 to guarantee that the gap used to create the buckets is meaningful.

    Then there will be at most m = (u - l) / gap + 1 buckets. For each number num, it will fall in the k = (num - l) / gap bucket. After putting all elements of nums in the corresponding buckets, we can just scan the buckets to compute the maximum gap.

    The maximum gap is only dependent on the maximum number of the current bucket and the minimum number of the next neighboring bucket (the bucket should not be empty). So we only store the minimum and the maximum of each bucket. Each bucket is initialized as {minimum = INT_MAX, maximum = INT_MIN} and then updated while updating the buckets.

    Putting these together, we can have the following solution, barely a straight-forward implementation of the suggested solution.

    class Solution {
    public:
        int maximumGap(vector<int>& nums) {
            int n = nums.size();
            if (n < 2) return 0;
            auto lu = minmax_element(nums.begin(), nums.end());
            int l = *lu.first, u = *lu.second;
            int gap = max((u - l) / (n - 1), 1);
            int m = (u - l) / gap + 1;
            vector<int> bucketsMin(m, INT_MAX);
            vector<int> bucketsMax(m, INT_MIN);
            for (int num : nums) {
                int k = (num - l) / gap;
                if (num < bucketsMin[k]) bucketsMin[k] = num;
                if (num > bucketsMax[k]) bucketsMax[k] = num;
            }
            int i = 0, j; 
            gap = bucketsMax[0] - bucketsMin[0];
            while (i < m) {
                j = i + 1;
                while (j < m && bucketsMin[j] == INT_MAX && bucketsMax[j] == INT_MIN)
                    j++;
                if (j == m) break;
                gap = max(gap, bucketsMin[j] - bucketsMax[i]);
                i = j;
            }
            return gap;
        }
    };

  • 1

    I think the final part is only O(n2). Imagine a case where all numbers fall into the first or last bucket, not in any of the middle buckets.

    And just another way to find l and u:

        auto lu = minmax_element(begin(nums), end(nums));
        int l = *lu.first, u = *lu.second;
    

  • 0

    Hi, Stefan. Thank you for the nice remind. I have updated the final part and I guess it is of O(n) now. BTW, thank you for the minmax_element :-)


  • 0

    Yes, now it's O(n). Sadly it didn't help, same 16ms as before. Maybe we should add a test case that would TLE the original solution...


  • 0

    Hi, Stefan. I separate the original 2d buckets into two 1d bucketsMin and bucketsMax and the code is now 12ms.


  • 0

    So improving O(n^2) to O(n) didn't improve the time but such a boring optimization did?
    That's just wrong :-P


  • 0
    S

    There is a typo: in your explaination, "k = (num - u) / gap" should be "k = (num - l) / gap".


  • 0

    Hi, starfall. Thanks! I've updated it :-)


  • 1
    O

    1st time to know there is a minmax_element in std!!


  • 1

    @ourlord Haha, in fact, I learnt this from Stefan.


  • 0

    I think there is no need to check both values bucketsMin[j] == INT_MAX && bucketsMax[j] == INT_MIN. Because they will be both true or both false.


  • 0
    Y
    This post is deleted!

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.