@@ -41,3 +41,80 @@ MyCalendarThree.prototype.book = function(start, end) {
41
41
* var obj = new MyCalendarThree()
42
42
* var param_1 = obj.book(start,end)
43
43
*/
44
+
45
+ // another
46
+
47
+
48
+ var MyCalendarThree = function ( ) {
49
+ this . st = new SegmentTree ( 0 , 10 ** 9 ) ;
50
+ } ;
51
+
52
+ /**
53
+ * @param {number } start
54
+ * @param {number } end
55
+ * @return {number }
56
+ */
57
+ MyCalendarThree . prototype . book = function ( start , end ) {
58
+ this . st . add ( start , end ) ;
59
+ return this . st . getMax ( ) ;
60
+ } ;
61
+
62
+ /**
63
+ * Your MyCalendarThree object will be instantiated and called as such:
64
+ * var obj = new MyCalendarThree()
65
+ * var param_1 = obj.book(start,end)
66
+ */
67
+
68
+ class SegmentTree {
69
+ constructor ( start , end ) {
70
+ this . root = new TreeNode ( start , end ) ;
71
+ }
72
+
73
+ add ( qs , qe , node = this . root ) {
74
+
75
+ // completely outside of query range
76
+ if ( qs > node . end || qe <= node . start ) {
77
+ return node . val ;
78
+ }
79
+
80
+ // completely covered by query range
81
+ if ( qs <= node . start && qe > node . end ) {
82
+ node . booked += 1 ;
83
+ node . val += 1 ;
84
+ return node . val ;
85
+ }
86
+
87
+ let mid = ( node . start + node . end ) / 2 >> 0 ;
88
+
89
+ if ( ! node . left ) {
90
+ node . left = new TreeNode ( node . start , mid ) ;
91
+ }
92
+
93
+ if ( ! node . right ) {
94
+ node . right = new TreeNode ( mid + 1 , node . end ) ;
95
+ }
96
+
97
+ node . val = Math . max (
98
+ this . add ( qs , qe , node . left ) ,
99
+ this . add ( qs , qe , node . right ) ,
100
+ ) + node . booked ;
101
+
102
+ return node . val ;
103
+
104
+ }
105
+
106
+ getMax ( ) {
107
+ return this . root . val ;
108
+ }
109
+
110
+ }
111
+
112
+ class TreeNode {
113
+ constructor ( start , end ) {
114
+ this . start = start ;
115
+ this . end = end ;
116
+ this . val = 0 ;
117
+ this . booked = 0 ;
118
+ this . left = this . right = null ;
119
+ }
120
+ }
0 commit comments