File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change 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+ */
You can’t perform that action at this time.
0 commit comments