-
Notifications
You must be signed in to change notification settings - Fork 1
/
reverse-pairs.ts
48 lines (44 loc) · 1.02 KB
/
reverse-pairs.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
let count = 0
export const resetCount = () => {
count = 0
}
/**
* 归并排序 - 合并左右
*/
export const merge = (left: number[], right: number[]) => {
const res = []
const leftLength = left.length
const rightLength = right.length
for (
let index = 0, l = 0, r = 0;
index < leftLength + rightLength;
index++
) {
if (l >= leftLength) res[index] = right[r++]
else if (r >= rightLength) res[index] = left[l++]
else if (left[l] <= right[r]) res[index] = left[l++]
else {
res[index] = right[r++]
count += leftLength - l // 唯一与归并排序有差异的地方
}
}
return res
}
/**
* 归并排序
*/
export const mergeSort = (nums: number[]): number[] => {
if (nums.length < 2) return nums
const mid = ~~(nums.length / 2)
const left = nums.slice(0, mid)
const right = nums.slice(mid)
return merge(mergeSort(left), mergeSort(right))
}
/**
* 逆序对
*/
export const reversePairs = function (nums: number[]): number {
resetCount()
mergeSort(nums)
return count
}