forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution2.ts
67 lines (63 loc) · 1.55 KB
/
Solution2.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
class BinaryIndexedTree {
private n: number;
private c: number[];
constructor(n: number) {
this.n = n;
this.c = new Array(n + 1).fill(0);
}
public update(x: number, v: number): void {
while (x <= this.n) {
this.c[x] += v;
x += x & -x;
}
}
public query(x: number): number {
let s = 0;
while (x > 0) {
s += this.c[x];
x -= x & -x;
}
return s;
}
}
function numTeams(rating: number[]): number {
let nums = [...rating];
nums.sort((a, b) => a - b);
const n = rating.length;
let m = 0;
for (let i = 0; i < n; ++i) {
if (i === 0 || nums[i] !== nums[i - 1]) {
nums[m++] = nums[i];
}
}
nums = nums.slice(0, m);
const search = (x: number): number => {
let l = 0;
let r = m;
while (l < r) {
const mid = (l + r) >> 1;
if (nums[mid] >= x) {
r = mid;
} else {
l = mid + 1;
}
}
return l + 1;
};
let ans = 0;
const tree1 = new BinaryIndexedTree(m);
const tree2 = new BinaryIndexedTree(m);
for (const x of rating) {
tree2.update(search(x), 1);
}
for (let i = 0; i < n; ++i) {
const x = search(rating[i]);
tree1.update(x, 1);
tree2.update(x, -1);
const l = tree1.query(x - 1);
const r = n - i - 1 - tree2.query(x);
ans += l * r;
ans += (i - l) * (n - i - 1 - r);
}
return ans;
}