Description

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

Return 0 if the array contains less than 2 elements.

Example 1:

Input: [3,6,9,1]
Output: 3
Explanation: The sorted form of the array is [1,3,6,9], either
             (3,6) or (6,9) has the maximum difference 3.

Example 2:

Input: [10]
Output: 0
Explanation: The array contains less than 2 elements, therefore return 0.

Note:

  • You may assume all elements in the array are non-negative integers and fit in the 32-bit signed integer range.
  • Try to solve it in linear time/space.

Solutions

1. Bucket Sort

# Time: O(n)
# Space: O(n)
class Solution:
    def maximumGap(self, nums: List[int]) -> int:
        if len(nums) < 2 or min(nums) == max(nums):
            return 0
        min_v, max_v = min(nums), max(nums)
        size = math.ceil((max_v - min_v) / (len(nums) - 1))
        # bucket store min_v and max_v
        bucket = [[None, None] for _ in range((max_v - min_v) // size + 1)]
        for num in nums:
            bckt = bucket[(num - min_v) // size]
            bckt[0] = num if bckt[0] is None else min(bckt[0], num)
            bckt[1] = num if bckt[1] is None else max(bckt[1], num)
        bucket = [bckt for bckt in bucket if bckt[0] is not None]
        
        res = 0
        for i in range(1, len(bucket)):
            res = max(res, bucket[i][0] - bucket[i - 1][1])
        return res
# 18/18 cases passed (68 ms)
# Your runtime beats 53.43 % of python3 submissions
# Your memory usage beats 66.67 % of python3 submissions (13.7 MB)

References

  1. 164. Maximum Gap