<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>

class Solution:
    def maximumGap(self, nums: List[int]) -> int:
        if len(nums) in [0, 1]:
            return 0
        
        # Based on Pigeonhole Principle, the maximum gap is at least (max - min) / (n - 1)
        n = len(nums)
        maximum, minimum = max(nums), min(nums) # 9, 1
        bucket_size = max(1, (maximum - minimum) // (n - 1)) # 2
        num_buckets = (maximum - minimum) // bucket_size + 1# 4
        # [1, 3] & [3, 5] & [5, 7] & [7, 9]

        buckets = [[float('inf'), float('-inf')] for i in range(num_buckets)]
        
        # Initialize the buckets
        buckets[0][0], buckets[-1][1] = minimum, maximum

        # Place numbers into buckets and update the bucket information
        for num in nums: # 3, 6, 9, 1
            # if num == maximum:
            #     continue
            bucket_idx = (num - minimum) // bucket_size # 1, 2, 4, 0
            # if bucket_idx == num_buckets: # Maximum number is visited
            #     bucket_idx -= 1
            buckets[bucket_idx][0] = min(buckets[bucket_idx][0], num)
            buckets[bucket_idx][1] = max(buckets[bucket_idx][1], num)

        # Iterate all valid buckets and update the gap by subtracting the previous maximum number from the minimum of the current bucket        
        prev_max = minimum
        maximum_gap = 0
        for i in range(num_buckets):
            if buckets[i][0] == float('inf'):
                # Not updated, thus invalid (empty)
                continue
            
            mini, maxi = buckets[i][0], buckets[i][1]
            maximum_gap = max(maximum_gap, mini - prev_max)
            prev_max =  maxi
        # print(buckets)
        return maximum_gap


38731