forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsounmind.js
40 lines (35 loc) ยท 931 Bytes
/
sounmind.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* Definition of Interval:
* class Interval {
* constructor(start, end) {
* this.start = start;
* this.end = end;
* }
* }
*/
export class Solution {
/**
* @param intervals: an array of meeting time intervals
* @return: if a person could attend all meetings
*/
canAttendMeetings(intervals) {
intervals.sort((a, b) => a[0] - b[0]);
for (let i = 1; i < intervals.length; i++) {
const startTime = intervals[i][0];
const previousMeetingEndTime = intervals[i - 1][1];
if (previousMeetingEndTime > startTime) {
return false;
}
}
return true;
}
}
/**
* Time complexity: O(nlogn), where n is the number of meetings
* Reason:
* We sort the meetings by start time, which takes O(nlogn) time.
* Then we iterate through the meetings once, which takes O(n) time.
*
* Space complexity: O(1)
* Reason: We use a constant amount of extra space.
*/