-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyCalendarTwo.java
113 lines (90 loc) · 2.94 KB
/
MyCalendarTwo.java
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**
* @author Lillard
*/
class MyCalendarTwo {
private static final int MAX_VALUE = 1000000000;
private class TreeNode {
private int startInclusive;
private int mid;
private int endExclusive;
private int maxValue; // max value from [startInclusive, endExclusive)
private int lazyDelta; // lazy update
private TreeNode left;
private TreeNode right;
TreeNode(int startInclusive, int endExclusive, int maxValue) {
this.startInclusive = startInclusive;
this.endExclusive = endExclusive;
this.mid = (startInclusive + endExclusive) / 2;
this.maxValue = maxValue;
this.lazyDelta = 0;
this.left = null;
this.right = null;
}
private int findMax(int startInclusive, int endExclusive) {
if (startInclusive >= endExclusive) {
return 0;
}
if (this.startInclusive == startInclusive && this.endExclusive == endExclusive) {
return this.maxValue + this.lazyDelta;
}
int max = 0;
if (startInclusive < this.mid) {
int leftMax = this.getLeft().findMax(startInclusive, Math.min(this.mid, endExclusive));
max = Math.max(max, leftMax);
}
if (endExclusive > this.mid) {
int rightMax = this.getRight().findMax(Math.max(this.mid, startInclusive), endExclusive);
max = Math.max(max, rightMax);
}
return max + this.lazyDelta;
}
private void insert(int startInclusive, int endExclusive) {
if (startInclusive >= endExclusive) {
return;
}
if (this.startInclusive == startInclusive && this.endExclusive == endExclusive) {
this.lazyDelta++;
return;
}
// sink lazy delta
this.sink();
if (startInclusive < this.mid) {
this.getLeft().insert(startInclusive, Math.min(this.mid, endExclusive));
}
if (endExclusive > this.mid) {
this.getRight().insert(Math.max(this.mid, startInclusive), endExclusive);
}
this.maxValue = Math.max(this.getLeft().maxValue + this.getLeft().lazyDelta,
this.getRight().maxValue + this.getRight().lazyDelta);
}
private void sink() {
this.getLeft().lazyDelta += this.lazyDelta;
this.getRight().lazyDelta += this.lazyDelta;
this.lazyDelta = 0;
}
private TreeNode getLeft() {
if (this.left == null) {
this.left = new TreeNode(this.startInclusive, this.mid, this.maxValue);
}
return this.left;
}
private TreeNode getRight() {
if (this.right == null) {
this.right = new TreeNode(this.mid, this.endExclusive, this.maxValue);
}
return this.right;
}
}
private TreeNode root;
public MyCalendarTwo() {
this.root = new TreeNode(0, MAX_VALUE, 0);
}
public boolean book(int startInclusive, int endExclusive) {
if (root.findMax(startInclusive, endExclusive) < 2) {
root.insert(startInclusive, endExclusive);
return true;
} else {
return false;
}
}
}