-
Notifications
You must be signed in to change notification settings - Fork 1
/
maximum-gap.ts
97 lines (84 loc) · 2.01 KB
/
maximum-gap.ts
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// LeetCode 164. 最大间距 https://leetcode-cn.com/problems/maximum-gap/
// LintCode 400. 最大间距 https://www.lintcode.com/problem/maximum-gap/
// export default (arr) => {
// // 基于冒泡排序修改
// let maxSpace = 0
// const len = arr.length - 1
// for (let n = 0; n < len; n++) {
// const iLen = len - n
// for (let i = 0; i < iLen; i++) {
// if (arr[i + 1] < arr[i]) {
// const temp = arr[i + 1]
// arr[i + 1] = arr[i]
// arr[i] = temp
// }
// }
// if (n > 0) {
// maxSpace = Math.max(arr[iLen + 1] - arr[iLen], maxSpace)
// }
// }
// return len > 0 ? Math.max(maxSpace, arr[1] - arr[0]) : 0
// }
export default function maximumGap (nums: number[]) {
const min = (a: number, b: number) => {
if (a === -1) {
return b
} else if (b === -1) {
return a
} else if (a < b) {
return a
} else {
return b
}
}
const max = (a: number, b: number) => {
if (a === -1) {
return b
} else if (b === -1) {
return a
} else if (a > b) {
return a
} else {
return b
}
}
if (nums.length < 2) {
return 0
}
let minNum = -1
let maxNum = -1
const n = nums.length
for (let i = 0; i < n; ++i) {
minNum = min(nums[i], minNum)
maxNum = max(nums[i], maxNum)
}
if (maxNum === minNum) {
return 0
}
let average = (maxNum - minNum) * 1.0 / (n - 1)
// if (average === 0) {
// ++average
// }
const localMin = new Array(n).fill(-1)
const localMax = new Array(n).fill(-1)
for (let i = 0; i < n; ++i) {
const t = (((nums[i] - minNum) / average) | 0)
localMin[t] = min(localMin[t], nums[i])
localMax[t] = max(localMax[t], nums[i])
}
let ans = (average | 0)
let left = 0
let right = 1
while ((left < n - 1)) {
while ((right < n && localMin[right] === -1)) {
++right
}
// if (right >= n) {
// break
// }
ans = max(ans, localMin[right] - localMax[left])
left = right
++right
}
return ans
}