题目:
题解:
type pair struct{ min, max int }func maximumGap(nums []int) (ans int) {n := len(nums)if n < 2 {return}minVal := min(nums...)maxVal := max(nums...)d := max(1, (maxVal-minVal)/(n-1))bucketSize := (maxVal-minVal)/d + 1// 存储 (桶内最小值,桶内最大值) 对,(-1, -1) 表示该桶是空的buckets := make([]pair, bucketSize)for i := range buckets {buckets[i] = pair{-1, -1}}for _, v := range nums {bid := (v - minVal) / dif buckets[bid].min == -1 {buckets[bid].min = vbuckets[bid].max = v} else {buckets[bid].min = min(buckets[bid].min, v)buckets[bid].max = max(buckets[bid].max, v)}}prev := -1for i, b := range buckets {if b.min == -1 {continue}if prev != -1 {ans = max(ans, b.min-buckets[prev].max)}prev = i}return
}func min(a ...int) int {res := a[0]for _, v := range a[1:] {if v < res {res = v}}return res
}func max(a ...int) int {res := a[0]for _, v := range a[1:] {if v > res {res = v}}return res
}