-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprob0164.go
60 lines (54 loc) · 1.27 KB
/
prob0164.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package prob0164
// 题目说的是排序以后的相邻两个数的最大差值
func maximumGap(nums []int) int {
n := len(nums)
if n < 2 {
return 0
}
minv, maxv := nums[0], nums[0]
for _, num := range nums {
minv = min(minv, num)
maxv = max(maxv, num)
}
// 分桶步长要让相邻两个数的最值不在一个桶内,最值一定比平均值要大
span := (maxv-minv)/n + 1
bucketNum := (maxv-minv)/span + 1
buckets := make([][]int, 0, bucketNum)
for i := 0; i < bucketNum; i++ {
buckets = append(buckets, make([]int, 0))
}
// 最值一定出现在两个桶的边缘
for _, num := range nums {
bucketIndex := (num - minv) / span
if len(buckets[bucketIndex]) == 0 {
buckets[bucketIndex] = append(buckets[bucketIndex], num)
buckets[bucketIndex] = append(buckets[bucketIndex], num)
} else {
buckets[bucketIndex][0] = min(buckets[bucketIndex][0], num)
buckets[bucketIndex][1] = max(buckets[bucketIndex][1], num)
}
}
res, preIndex := 0, 0
for i := 1; i < bucketNum; i++ {
if len(buckets[i]) == 0 {
continue
}
res = max(res, buckets[i][0]-buckets[preIndex][1])
preIndex = i
}
return res
}
func min(a, b int) int {
if a < b {
return a
} else {
return b
}
}
func max(a, b int) int {
if a < b {
return b
} else {
return a
}
}