-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.ts
44 lines (36 loc) · 891 Bytes
/
Solution.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
class BinaryIndexedTree {
private n: number;
private c: number[];
constructor(n: number) {
this.n = n;
this.c = Array(n + 1).fill(0);
}
update(x: number, delta: number): void {
for (; x <= this.n; x += x & -x) {
this.c[x] += delta;
}
}
query(x: number): number {
let s = 0;
for (; x > 0; x -= x & -x) {
s += this.c[x];
}
return s;
}
}
class StreamRank {
private tree: BinaryIndexedTree = new BinaryIndexedTree(50010);
constructor() {}
track(x: number): void {
this.tree.update(x + 1, 1);
}
getRankOfNumber(x: number): number {
return this.tree.query(x + 1);
}
}
/**
* Your StreamRank object will be instantiated and called as such:
* var obj = new StreamRank()
* obj.track(x)
* var param_2 = obj.getRankOfNumber(x)
*/