Skip to content

Commit 94c2f58

Browse files
committed
array - O(nlogn) O(n)
1 parent f1b4436 commit 94c2f58

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

leetcode-252-meetingRooms.js

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
// Title: Meeting Rooms
2+
// Difficulty: Medium
3+
// Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), determine if a person could attend all meetings.
4+
5+
// Example 1:
6+
// Input: [[0,30],[5,10],[15,20]]
7+
// Output: false
8+
// Explanation:
9+
// Meeting 1: [0, 30]
10+
// Meeting 2: [5, 10]
11+
// Meeting 3: [15, 20]
12+
// The person cannot attend all meetings because meeting 2 starts while meeting 1 is still in progress.
13+
14+
// Example 2:
15+
// Input: [[7,10],[2,4]]
16+
// Output: true
17+
// Explanation:
18+
// Meeting 1: [7, 10]
19+
// Meeting 2: [2, 4]
20+
// The person can attend both meetings because meeting 2 starts before meeting 1 ends.
21+
22+
// Note:
23+
24+
// The number of meetings is at least 2 and at most 10^4.
25+
// The input array will always have a non-empty solution and each element of the array will have a positive duration.
26+
// The input array is already sorted in ascending order by start time.
27+
28+
// p: arr;
29+
// r: boolean;
30+
31+
// e: [[0,30],[5,10],[15,20]]
32+
//
33+
34+
const meetingRooms = (a) => {
35+
a.sort((i, j) => i[0] - j[0]);
36+
37+
for (let i = 1; i < a.length; i++) {
38+
if (a[i - 1][1] > a[i][0]) return false;
39+
}
40+
41+
return true;
42+
};
43+
44+
console.log(
45+
meetingRooms([
46+
[0, 30],
47+
[5, 10],
48+
[15, 20],
49+
])
50+
);
51+
52+
console.log(
53+
meetingRooms([
54+
[7, 10],
55+
[2, 4],
56+
])
57+
);
58+
59+
console.log(
60+
meetingRooms([
61+
[1, 5],
62+
[2, 3],
63+
[4, 6],
64+
])
65+
);
66+
console.log(
67+
meetingRooms([
68+
[1, 5],
69+
[2, 5],
70+
[3, 5],
71+
])
72+
);
73+
console.log(
74+
meetingRooms([
75+
[1, 2],
76+
[2, 3],
77+
[3, 4],
78+
])
79+
);
80+
console.log(
81+
meetingRooms([
82+
[1, 3],
83+
[2, 4],
84+
[3, 5],
85+
])
86+
);
87+
console.log(
88+
meetingRooms([
89+
[1, 3],
90+
[2, 4],
91+
[4, 5],
92+
])
93+
);
94+
console.log(
95+
meetingRooms([
96+
[1, 2],
97+
[1, 2],
98+
[1, 2],
99+
])
100+
);
101+
console.log(
102+
meetingRooms([
103+
[1, 4],
104+
[4, 8],
105+
[8, 12],
106+
])
107+
);
108+
console.log(
109+
meetingRooms([
110+
[1, 4],
111+
[4, 8],
112+
[8, 12],
113+
[12, 16],
114+
])
115+
);

0 commit comments

Comments
 (0)