Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problems: No.0252,0253 #4280

Merged
merged 1 commit into from
Mar 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 9 additions & 30 deletions solution/0200-0299/0252.Meeting Rooms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ tags:

### 方法一:排序

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

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

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

Expand All @@ -77,9 +77,7 @@ class Solution {
public boolean canAttendMeetings(int[][] intervals) {
Arrays.sort(intervals, (a, b) -> a[0] - b[0]);
for (int i = 1; i < intervals.length; ++i) {
var a = intervals[i - 1];
var b = intervals[i];
if (a[1] > b[0]) {
if (intervals[i - 1][1] > intervals[i][0]) {
return false;
}
}
Expand All @@ -94,11 +92,11 @@ class Solution {
class Solution {
public:
bool canAttendMeetings(vector<vector<int>>& intervals) {
sort(intervals.begin(), intervals.end(), [](const vector<int>& a, const vector<int>& b) {
ranges::sort(intervals, [](const auto& a, const auto& b) {
return a[0] < b[0];
});
for (int i = 1; i < intervals.size(); ++i) {
if (intervals[i][0] < intervals[i - 1][1]) {
if (intervals[i - 1][1] > intervals[i][0]) {
return false;
}
}
Expand Down Expand Up @@ -141,32 +139,13 @@ function canAttendMeetings(intervals: number[][]): boolean {

```rust
impl Solution {
#[allow(dead_code)]
pub fn can_attend_meetings(intervals: Vec<Vec<i32>>) -> bool {
if intervals.len() == 1 {
return true;
}

let mut intervals = intervals;

// Sort the intervals vector
intervals.sort_by(|lhs, rhs| lhs[0].cmp(&rhs[0]));

let mut end = -1;

// Begin traverse
for p in &intervals {
if end == -1 {
// This is the first pair
end = p[1];
continue;
}
if p[0] < end {
pub fn can_attend_meetings(mut intervals: Vec<Vec<i32>>) -> bool {
intervals.sort_by(|a, b| a[0].cmp(&b[0]));
for i in 1..intervals.len() {
if intervals[i - 1][1] > intervals[i][0] {
return false;
}
end = p[1];
}

true
}
}
Expand Down
43 changes: 14 additions & 29 deletions solution/0200-0299/0252.Meeting Rooms/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Sorting

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.

If no overlap is found by the end of the iteration, we return `true`.

The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$, where $n$ is the number of meetings.

<!-- tabs:start -->

Expand All @@ -62,9 +68,7 @@ class Solution {
public boolean canAttendMeetings(int[][] intervals) {
Arrays.sort(intervals, (a, b) -> a[0] - b[0]);
for (int i = 1; i < intervals.length; ++i) {
var a = intervals[i - 1];
var b = intervals[i];
if (a[1] > b[0]) {
if (intervals[i - 1][1] > intervals[i][0]) {
return false;
}
}
Expand All @@ -79,11 +83,11 @@ class Solution {
class Solution {
public:
bool canAttendMeetings(vector<vector<int>>& intervals) {
sort(intervals.begin(), intervals.end(), [](const vector<int>& a, const vector<int>& b) {
ranges::sort(intervals, [](const auto& a, const auto& b) {
return a[0] < b[0];
});
for (int i = 1; i < intervals.size(); ++i) {
if (intervals[i][0] < intervals[i - 1][1]) {
if (intervals[i - 1][1] > intervals[i][0]) {
return false;
}
}
Expand Down Expand Up @@ -126,32 +130,13 @@ function canAttendMeetings(intervals: number[][]): boolean {

```rust
impl Solution {
#[allow(dead_code)]
pub fn can_attend_meetings(intervals: Vec<Vec<i32>>) -> bool {
if intervals.len() == 1 {
return true;
}

let mut intervals = intervals;

// Sort the intervals vector
intervals.sort_by(|lhs, rhs| lhs[0].cmp(&rhs[0]));

let mut end = -1;

// Begin traverse
for p in &intervals {
if end == -1 {
// This is the first pair
end = p[1];
continue;
}
if p[0] < end {
pub fn can_attend_meetings(mut intervals: Vec<Vec<i32>>) -> bool {
intervals.sort_by(|a, b| a[0].cmp(&b[0]));
for i in 1..intervals.len() {
if intervals[i - 1][1] > intervals[i][0] {
return false;
}
end = p[1];
}

true
}
}
Expand Down
6 changes: 3 additions & 3 deletions solution/0200-0299/0252.Meeting Rooms/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
class Solution {
public:
bool canAttendMeetings(vector<vector<int>>& intervals) {
sort(intervals.begin(), intervals.end(), [](const vector<int>& a, const vector<int>& b) {
ranges::sort(intervals, [](const auto& a, const auto& b) {
return a[0] < b[0];
});
for (int i = 1; i < intervals.size(); ++i) {
if (intervals[i][0] < intervals[i - 1][1]) {
if (intervals[i - 1][1] > intervals[i][0]) {
return false;
}
}
return true;
}
};
};
6 changes: 2 additions & 4 deletions solution/0200-0299/0252.Meeting Rooms/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ class Solution {
public boolean canAttendMeetings(int[][] intervals) {
Arrays.sort(intervals, (a, b) -> a[0] - b[0]);
for (int i = 1; i < intervals.length; ++i) {
var a = intervals[i - 1];
var b = intervals[i];
if (a[1] > b[0]) {
if (intervals[i - 1][1] > intervals[i][0]) {
return false;
}
}
return true;
}
}
}
27 changes: 4 additions & 23 deletions solution/0200-0299/0252.Meeting Rooms/Solution.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,11 @@
impl Solution {
#[allow(dead_code)]
pub fn can_attend_meetings(intervals: Vec<Vec<i32>>) -> bool {
if intervals.len() == 1 {
return true;
}

let mut intervals = intervals;

// Sort the intervals vector
intervals.sort_by(|lhs, rhs| lhs[0].cmp(&rhs[0]));

let mut end = -1;

// Begin traverse
for p in &intervals {
if end == -1 {
// This is the first pair
end = p[1];
continue;
}
if p[0] < end {
pub fn can_attend_meetings(mut intervals: Vec<Vec<i32>>) -> bool {
intervals.sort_by(|a, b| a[0].cmp(&b[0]));
for i in 1..intervals.len() {
if intervals[i - 1][1] > intervals[i][0] {
return false;
}
end = p[1];
}

true
}
}
Loading