Skip to content

Commit 9ce6b20

Browse files
authored
feat: add solutions to lc problems: No.0252,0253 (#4280)
* No.0252.Meeting Rooms * No.0253.Meeting Rooms II
1 parent 7842612 commit 9ce6b20

19 files changed

+709
-269
lines changed

solution/0200-0299/0252.Meeting Rooms/README.md

+9-30
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ tags:
5353

5454
### 方法一:排序
5555

56-
我们将会议按照开始时间进行排序,然后遍历排序后的会议,如果当前会议的开始时间小于前一个会议的结束时间,则说明两个会议有重叠,返回 `false` 即可
56+
我们将会议按照开始时间进行排序,然后遍历排序后的会议,如果当前会议的开始时间小于前一个会议的结束时间,则说明两个会议有重叠,返回 $\text{false}$,否则继续遍历
5757

58-
遍历结束后,返回 `true`
58+
如果遍历结束都没有发现重叠的会议,则返回 $\text{true}$
5959

6060
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为会议数量。
6161

@@ -77,9 +77,7 @@ class Solution {
7777
public boolean canAttendMeetings(int[][] intervals) {
7878
Arrays.sort(intervals, (a, b) -> a[0] - b[0]);
7979
for (int i = 1; i < intervals.length; ++i) {
80-
var a = intervals[i - 1];
81-
var b = intervals[i];
82-
if (a[1] > b[0]) {
80+
if (intervals[i - 1][1] > intervals[i][0]) {
8381
return false;
8482
}
8583
}
@@ -94,11 +92,11 @@ class Solution {
9492
class Solution {
9593
public:
9694
bool canAttendMeetings(vector<vector<int>>& intervals) {
97-
sort(intervals.begin(), intervals.end(), [](const vector<int>& a, const vector<int>& b) {
95+
ranges::sort(intervals, [](const auto& a, const auto& b) {
9896
return a[0] < b[0];
9997
});
10098
for (int i = 1; i < intervals.size(); ++i) {
101-
if (intervals[i][0] < intervals[i - 1][1]) {
99+
if (intervals[i - 1][1] > intervals[i][0]) {
102100
return false;
103101
}
104102
}
@@ -141,32 +139,13 @@ function canAttendMeetings(intervals: number[][]): boolean {
141139

142140
```rust
143141
impl Solution {
144-
#[allow(dead_code)]
145-
pub fn can_attend_meetings(intervals: Vec<Vec<i32>>) -> bool {
146-
if intervals.len() == 1 {
147-
return true;
148-
}
149-
150-
let mut intervals = intervals;
151-
152-
// Sort the intervals vector
153-
intervals.sort_by(|lhs, rhs| lhs[0].cmp(&rhs[0]));
154-
155-
let mut end = -1;
156-
157-
// Begin traverse
158-
for p in &intervals {
159-
if end == -1 {
160-
// This is the first pair
161-
end = p[1];
162-
continue;
163-
}
164-
if p[0] < end {
142+
pub fn can_attend_meetings(mut intervals: Vec<Vec<i32>>) -> bool {
143+
intervals.sort_by(|a, b| a[0].cmp(&b[0]));
144+
for i in 1..intervals.len() {
145+
if intervals[i - 1][1] > intervals[i][0] {
165146
return false;
166147
}
167-
end = p[1];
168148
}
169-
170149
true
171150
}
172151
}

solution/0200-0299/0252.Meeting Rooms/README_EN.md

+14-29
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,13 @@ tags:
4242

4343
<!-- solution:start -->
4444

45-
### Solution 1
45+
### Solution 1: Sorting
46+
47+
We sort the meetings based on their start times, and then iterate through the sorted meetings. If the start time of the current meeting is less than the end time of the previous meeting, it indicates that there is an overlap between the two meetings, and we return `false`. Otherwise, we continue iterating.
48+
49+
If no overlap is found by the end of the iteration, we return `true`.
50+
51+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$, where $n$ is the number of meetings.
4652

4753
<!-- tabs:start -->
4854

@@ -62,9 +68,7 @@ class Solution {
6268
public boolean canAttendMeetings(int[][] intervals) {
6369
Arrays.sort(intervals, (a, b) -> a[0] - b[0]);
6470
for (int i = 1; i < intervals.length; ++i) {
65-
var a = intervals[i - 1];
66-
var b = intervals[i];
67-
if (a[1] > b[0]) {
71+
if (intervals[i - 1][1] > intervals[i][0]) {
6872
return false;
6973
}
7074
}
@@ -79,11 +83,11 @@ class Solution {
7983
class Solution {
8084
public:
8185
bool canAttendMeetings(vector<vector<int>>& intervals) {
82-
sort(intervals.begin(), intervals.end(), [](const vector<int>& a, const vector<int>& b) {
86+
ranges::sort(intervals, [](const auto& a, const auto& b) {
8387
return a[0] < b[0];
8488
});
8589
for (int i = 1; i < intervals.size(); ++i) {
86-
if (intervals[i][0] < intervals[i - 1][1]) {
90+
if (intervals[i - 1][1] > intervals[i][0]) {
8791
return false;
8892
}
8993
}
@@ -126,32 +130,13 @@ function canAttendMeetings(intervals: number[][]): boolean {
126130

127131
```rust
128132
impl Solution {
129-
#[allow(dead_code)]
130-
pub fn can_attend_meetings(intervals: Vec<Vec<i32>>) -> bool {
131-
if intervals.len() == 1 {
132-
return true;
133-
}
134-
135-
let mut intervals = intervals;
136-
137-
// Sort the intervals vector
138-
intervals.sort_by(|lhs, rhs| lhs[0].cmp(&rhs[0]));
139-
140-
let mut end = -1;
141-
142-
// Begin traverse
143-
for p in &intervals {
144-
if end == -1 {
145-
// This is the first pair
146-
end = p[1];
147-
continue;
148-
}
149-
if p[0] < end {
133+
pub fn can_attend_meetings(mut intervals: Vec<Vec<i32>>) -> bool {
134+
intervals.sort_by(|a, b| a[0].cmp(&b[0]));
135+
for i in 1..intervals.len() {
136+
if intervals[i - 1][1] > intervals[i][0] {
150137
return false;
151138
}
152-
end = p[1];
153139
}
154-
155140
true
156141
}
157142
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
class Solution {
22
public:
33
bool canAttendMeetings(vector<vector<int>>& intervals) {
4-
sort(intervals.begin(), intervals.end(), [](const vector<int>& a, const vector<int>& b) {
4+
ranges::sort(intervals, [](const auto& a, const auto& b) {
55
return a[0] < b[0];
66
});
77
for (int i = 1; i < intervals.size(); ++i) {
8-
if (intervals[i][0] < intervals[i - 1][1]) {
8+
if (intervals[i - 1][1] > intervals[i][0]) {
99
return false;
1010
}
1111
}
1212
return true;
1313
}
14-
};
14+
};

solution/0200-0299/0252.Meeting Rooms/Solution.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ class Solution {
22
public boolean canAttendMeetings(int[][] intervals) {
33
Arrays.sort(intervals, (a, b) -> a[0] - b[0]);
44
for (int i = 1; i < intervals.length; ++i) {
5-
var a = intervals[i - 1];
6-
var b = intervals[i];
7-
if (a[1] > b[0]) {
5+
if (intervals[i - 1][1] > intervals[i][0]) {
86
return false;
97
}
108
}
119
return true;
1210
}
13-
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,11 @@
11
impl Solution {
2-
#[allow(dead_code)]
3-
pub fn can_attend_meetings(intervals: Vec<Vec<i32>>) -> bool {
4-
if intervals.len() == 1 {
5-
return true;
6-
}
7-
8-
let mut intervals = intervals;
9-
10-
// Sort the intervals vector
11-
intervals.sort_by(|lhs, rhs| lhs[0].cmp(&rhs[0]));
12-
13-
let mut end = -1;
14-
15-
// Begin traverse
16-
for p in &intervals {
17-
if end == -1 {
18-
// This is the first pair
19-
end = p[1];
20-
continue;
21-
}
22-
if p[0] < end {
2+
pub fn can_attend_meetings(mut intervals: Vec<Vec<i32>>) -> bool {
3+
intervals.sort_by(|a, b| a[0].cmp(&b[0]));
4+
for i in 1..intervals.len() {
5+
if intervals[i - 1][1] > intervals[i][0] {
236
return false;
247
}
25-
end = p[1];
268
}
27-
289
true
2910
}
3011
}

0 commit comments

Comments
 (0)