Skip to content

Commit 0f6d2c0

Browse files
committed
solve: meeting rooms
1 parent d6e57fb commit 0f6d2c0

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

meeting-rooms/evan.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Definition of Interval:
3+
* class Interval {
4+
* constructor(start, end) {
5+
* this.start = start;
6+
* this.end = end;
7+
* }
8+
* }
9+
*/
10+
11+
export class Solution {
12+
/**
13+
* @param intervals: an array of meeting time intervals
14+
* @return: if a person could attend all meetings
15+
*/
16+
canAttendMeetings(intervals) {
17+
intervals.sort((a, b) => a[0] - b[0]);
18+
19+
for (let i = 1; i < intervals.length; i++) {
20+
const startTime = intervals[i][0];
21+
const previousMeetingEndTime = intervals[i - 1][1];
22+
23+
if (previousMeetingEndTime > startTime) {
24+
return false;
25+
}
26+
}
27+
28+
return true;
29+
}
30+
}
31+
32+
/**
33+
* Time complexity: O(nlogn), where n is the number of meetings
34+
* Reason:
35+
* We sort the meetings by start time, which takes O(nlogn) time.
36+
* Then we iterate through the meetings once, which takes O(n) time.
37+
*
38+
* Space complexity: O(1)
39+
* Reason: We use a constant amount of extra space.
40+
*/

0 commit comments

Comments
 (0)