File tree Expand file tree Collapse file tree 3 files changed +106
-0
lines changed
solution/0700-0799/0731.My Calendar II Expand file tree Collapse file tree 3 files changed +106
-0
lines changed Original file line number Diff line number Diff line change @@ -208,6 +208,43 @@ func (this *MyCalendarTwo) Book(start int, end int) bool {
208208 */
209209```
210210
211+ #### JavaScript
212+
213+ ``` js
214+ var MyCalendarTwo = function () {
215+ this .events = [];
216+ this .overlaps = [];
217+ };
218+
219+ /**
220+ * @param {number} start
221+ * @param {number} end
222+ * @return {boolean}
223+ */
224+ MyCalendarTwo .prototype .book = function (start , end ) {
225+ for (let [s, e] of this .overlaps ) {
226+ if (Math .max (start, s) < Math .min (end, e)) {
227+ return false ;
228+ }
229+ }
230+
231+ for (let [s, e] of this .events ) {
232+ if (Math .max (start, s) < Math .min (end, e)) {
233+ this .overlaps .push ([Math .max (start, s), Math .min (end, e)]);
234+ }
235+ }
236+
237+ this .events .push ([start, end]);
238+ return true ;
239+ };
240+
241+ /**
242+ * Your MyCalendarTwo object will be instantiated and called as such:
243+ * var obj = new MyCalendarTwo()
244+ * var param_1 = obj.book(start,end)
245+ */
246+ ```
247+
211248<!-- tabs:end -->
212249
213250<!-- solution:end -->
Original file line number Diff line number Diff line change @@ -206,6 +206,43 @@ func (this *MyCalendarTwo) Book(start int, end int) bool {
206206 */
207207```
208208
209+ #### JavaScript
210+
211+ ``` js
212+ var MyCalendarTwo = function () {
213+ this .events = [];
214+ this .overlaps = [];
215+ };
216+
217+ /**
218+ * @param {number} start
219+ * @param {number} end
220+ * @return {boolean}
221+ */
222+ MyCalendarTwo .prototype .book = function (start , end ) {
223+ for (let [s, e] of this .overlaps ) {
224+ if (Math .max (start, s) < Math .min (end, e)) {
225+ return false ;
226+ }
227+ }
228+
229+ for (let [s, e] of this .events ) {
230+ if (Math .max (start, s) < Math .min (end, e)) {
231+ this .overlaps .push ([Math .max (start, s), Math .min (end, e)]);
232+ }
233+ }
234+
235+ this .events .push ([start, end]);
236+ return true ;
237+ };
238+
239+ /**
240+ * Your MyCalendarTwo object will be instantiated and called as such:
241+ * var obj = new MyCalendarTwo()
242+ * var param_1 = obj.book(start,end)
243+ */
244+ ```
245+
209246<!-- tabs:end -->
210247
211248<!-- solution:end -->
Original file line number Diff line number Diff line change 1+ var MyCalendarTwo = function ( ) {
2+ this . events = [ ] ;
3+ this . overlaps = [ ] ;
4+ } ;
5+
6+ /**
7+ * @param {number } start
8+ * @param {number } end
9+ * @return {boolean }
10+ */
11+ MyCalendarTwo . prototype . book = function ( start , end ) {
12+ for ( let [ s , e ] of this . overlaps ) {
13+ if ( Math . max ( start , s ) < Math . min ( end , e ) ) {
14+ return false ;
15+ }
16+ }
17+
18+ for ( let [ s , e ] of this . events ) {
19+ if ( Math . max ( start , s ) < Math . min ( end , e ) ) {
20+ this . overlaps . push ( [ Math . max ( start , s ) , Math . min ( end , e ) ] ) ;
21+ }
22+ }
23+
24+ this . events . push ( [ start , end ] ) ;
25+ return true ;
26+ } ;
27+
28+ /**
29+ * Your MyCalendarTwo object will be instantiated and called as such:
30+ * var obj = new MyCalendarTwo()
31+ * var param_1 = obj.book(start,end)
32+ */
You can’t perform that action at this time.
0 commit comments