-
Notifications
You must be signed in to change notification settings - Fork 41
/
solution.ts
74 lines (66 loc) · 2.32 KB
/
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
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
const MAX = 10 ** 9;
class MyCalendarThree {
values:number[] = [0, ]
deltas:number[] = [0, ]
leftIndexs:number[] = []
rightIndexs:number[] = []
count = 1;
constructor () {
}
private pushDown (index:number) {
if (!this.leftIndexs[index]) {
const leftIndex = this.count++;
this.leftIndexs[index] = leftIndex;
this.values[leftIndex] = 0;
this.deltas[leftIndex] = 0;
}
if (!this.rightIndexs[index]) {
const rightIndex = this.count++;
this.rightIndexs[index] = rightIndex;
this.values[rightIndex] = 0;
this.deltas[rightIndex] = 0;
}
const leftIndex = this.leftIndexs[index];
const rightIndex = this.rightIndexs[index];
this.values[leftIndex] += this.deltas[index];
this.deltas[leftIndex] += this.deltas[index];
this.values[rightIndex] += this.deltas[index];
this.deltas[rightIndex] += this.deltas[index];
this.deltas[index] = 0;
}
private update (
rootIndex:number,
rangeL:number,
rangeR:number,
updateL:number,
updateR:number
) {
if (rangeL === updateL && rangeR === updateR) {
this.values[rootIndex]++;
this.deltas[rootIndex]++;
return;
}
this.pushDown(rootIndex);
const mid = Math.floor((rangeL + rangeR) / 2);
const leftRootIndex = this.leftIndexs[rootIndex];
const rightRootIndex = this.rightIndexs[rootIndex];
if (updateL > mid) {
this.update(rightRootIndex, mid + 1, rangeR, updateL, updateR);
} else if (updateR < mid + 1) {
this.update(leftRootIndex, rangeL, mid, updateL, updateR);
} else {
this.update(leftRootIndex, rangeL, mid, updateL, mid);
this.update(rightRootIndex, mid + 1, rangeR, mid + 1, updateR);
}
this.values[rootIndex] = Math.max(this.values[leftRootIndex], this.values[rightRootIndex]);
}
book (startTime: number, endTime: number): number {
this.update(0, 0, MAX, startTime, endTime - 1);
return this.values[0];
}
}
/**
* Your MyCalendarThree object will be instantiated and called as such:
* var obj = new MyCalendarThree()
* var param_1 = obj.book(startTime,endTime)
*/