-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountSmaller.ts
82 lines (67 loc) · 1.89 KB
/
countSmaller.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
// <Recursion, DivideAndConquer, Merge Sort>
// Time: O(nlogn)
// Space: O(n)
class MySortHelper {
private nums: number[]
private indexes: number[]
private temp: number[]
private counts: number[]
public constructor(nums: number[]) {
this.nums = nums
this.indexes = nums.map((_, index) => index)
this.temp = new Array(nums.length)
this.counts = new Array(nums.length).fill(0)
}
// 1. merge sort
private merge(left: number, mid: number, right: number) {
let i = left
let j = mid + 1
let p = 0
let count = 0
while (i <= mid && j <= right) {
if (this.nums[this.indexes[i]] <= this.nums[this.indexes[j]]) {
this.temp[p] = this.indexes[i]
this.counts[this.indexes[i]] += count
++p
++i
} else {
this.temp[p] = this.indexes[j]
++p
++j
++count
}
}
while (i <= mid) {
this.temp[p] = this.indexes[i]
this.counts[this.indexes[i]] += count
++p
++i
}
while (j <= right) {
this.temp[p] = this.indexes[j]
++p
++j
}
for (let k = 0; k < p; k++) {
this.indexes[left + k] = this.temp[k]
}
}
public sort(left = 0, right: number = this.nums.length - 1) {
if (left >= right) {
return
}
const mid = (left + right) >>> 1
this.sort(left, mid)
this.sort(mid + 1, right)
this.merge(left, mid, right)
}
public getCounts(): number[] {
return this.counts
}
}
function countSmaller(nums: number[]): number[] {
const mySortHelper = new MySortHelper(nums)
mySortHelper.sort()
return mySortHelper.getCounts()
}
export { countSmaller }