function maximumGap(nums: number[]): number {
if (nums.length < 2) return 0;
const k = Math.floor(nums.length / 3);
const buckets: number[][] = Array.from({ length: k + 1 }, () => []);
const min = Math.min(...nums);
const range = Math.max(...nums) - min;
if (range === 0) return 0;
for (const num of nums) {
buckets[Math.floor(((num - min) / range) * k)].push(num);
}
const sorted: number[] = [];
for (const bucket of buckets) {
sorted.push(...bucket.sort((a, b) => a - b));
}
let res = 0;
for (let i = 1; i < sorted.length; i++) {
res = Math.max(res, sorted[i] - sorted[i - 1]);
}
return res;
}