Skip to content

Commit 3af011a

Browse files
committed
Add solution #729
1 parent 8abe102 commit 3af011a

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,7 @@
552552
725|[Split Linked List in Parts](./0725-split-linked-list-in-parts.js)|Medium|
553553
726|[Number of Atoms](./0726-number-of-atoms.js)|Hard|
554554
728|[Self Dividing Numbers](./0728-self-dividing-numbers.js)|Easy|
555+
729|[My Calendar I](./0729-my-calendar-i.js)|Medium|
555556
733|[Flood Fill](./0733-flood-fill.js)|Easy|
556557
735|[Asteroid Collision](./0735-asteroid-collision.js)|Medium|
557558
739|[Daily Temperatures](./0739-daily-temperatures.js)|Medium|

solutions/0729-my-calendar-i.js

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* 729. My Calendar I
3+
* https://leetcode.com/problems/my-calendar-i/
4+
* Difficulty: Medium
5+
*
6+
* You are implementing a program to use as your calendar. We can add a new event if adding the
7+
* event will not cause a double booking.
8+
*
9+
* A double booking happens when two events have some non-empty intersection (i.e., some moment
10+
* is common to both events.).
11+
*
12+
* The event can be represented as a pair of integers startTime and endTime that represents a
13+
* booking on the half-open interval [startTime, endTime), the range of real numbers x such
14+
* that startTime <= x < endTime.
15+
*
16+
* Implement the MyCalendar class:
17+
* - MyCalendar() Initializes the calendar object.
18+
* - boolean book(int startTime, int endTime) Returns true if the event can be added to the
19+
* calendar successfully without causing a double booking. Otherwise, return false and do
20+
* not add the event to the calendar.
21+
*/
22+
23+
class MyCalendar {
24+
constructor() {
25+
this.events = [];
26+
}
27+
28+
/**
29+
* @param {number} startTime
30+
* @param {number} endTime
31+
* @returns {boolean}
32+
*/
33+
book(startTime, endTime) {
34+
const event = [startTime, endTime];
35+
const index = this.findInsertIndex(startTime);
36+
37+
if (this.overlapsPrevious(index - 1, startTime) || this.overlapsNext(index, endTime)) {
38+
return false;
39+
}
40+
41+
this.events.splice(index, 0, event);
42+
return true;
43+
}
44+
45+
/**
46+
* @param {number} startTime
47+
* @returns {number}
48+
*/
49+
findInsertIndex(startTime) {
50+
let left = 0;
51+
let right = this.events.length;
52+
53+
while (left < right) {
54+
const mid = Math.floor((left + right) / 2);
55+
if (this.events[mid][0] > startTime) {
56+
right = mid;
57+
} else {
58+
left = mid + 1;
59+
}
60+
}
61+
62+
return left;
63+
}
64+
65+
/**
66+
* @param {number} prevIndex
67+
* @param {number} startTime
68+
* @returns {boolean}
69+
*/
70+
overlapsPrevious(prevIndex, startTime) {
71+
return prevIndex >= 0 && this.events[prevIndex][1] > startTime;
72+
}
73+
74+
/**
75+
* @param {number} nextIndex
76+
* @param {number} endTime
77+
* @returns {boolean}
78+
*/
79+
overlapsNext(nextIndex, endTime) {
80+
return nextIndex < this.events.length && this.events[nextIndex][0] < endTime;
81+
}
82+
}

0 commit comments

Comments
 (0)