<p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>

<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [3,6,9,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The sorted form of the array is [1,3,6,9], either (3,6) or (6,9) has the maximum difference 3.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [10]
<strong>Output:</strong> 0
<strong>Explanation:</strong> The array contains less than 2 elements, therefore return 0.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
</ul>

"""
Time: O(n)
Space: O(n) => can reduce to O(1) by using LL instead of array to store buckets
"""
class Solution:
    def maximumGap(self, nums: List[int]) -> int:
        n = len(nums)
        if n < 2:
            return 0

        # bucket sort! don't have to worry about worst bucketsort scenario where all nums land in bucket1 and bucket2, because we look at the GAP 
        min_value = min(nums)
        max_value = max(nums)

        equal_spaces = (max_value - min_value) // (n-1)
        bucket_size = max(1, equal_spaces) # start off with equally spaced buckets, min max gap would be if they're all equally spaced apart (our answer can't lie within the same bucket)
        num_buckets = (max_value - min_value) // bucket_size + 1 

        # initalize buckets (min, max)
        buckets = [[float('inf'), -float('inf')] for _ in range(num_buckets)]
        for num in nums:
            bucket_index = (num - min_value) // bucket_size
            
            min_bucket = min(buckets[bucket_index][0], num)
            max_bucket = max(buckets[bucket_index][1], num)
            buckets[bucket_index][0] = min_bucket
            buckets[bucket_index][1] = max_bucket
        
        # compute max differences between buckets
        max_diff = 0
        prev_max = buckets[0][1]
        for i in range(1, num_buckets):
            if buckets[i][0] == float('inf'):
                continue
            
            max_diff = max(max_diff, buckets[i][0] - prev_max)
            prev_max = buckets[i][1]
        
        return max_diff
37693