Skip to content

Commit cb3592e

Browse files
authored
feat: add rust solution to lc problem: No.0253 (doocs#1237)
1 parent 9bd34c0 commit cb3592e

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed

solution/0200-0299/0253.Meeting Rooms II/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,48 @@ public:
9898
};
9999
```
100100
101+
### **Rust**
102+
103+
```rust
104+
use std::{collections::BinaryHeap, cmp::Reverse};
105+
106+
impl Solution {
107+
#[allow(dead_code)]
108+
pub fn min_meeting_rooms(intervals: Vec<Vec<i32>>) -> i32 {
109+
// The min heap that stores the earliest ending time among all meeting rooms
110+
let mut pq = BinaryHeap::new();
111+
let mut intervals = intervals;
112+
let n = intervals.len();
113+
114+
// Let's first sort the intervals vector
115+
intervals.sort_by(|lhs, rhs| {
116+
lhs[0].cmp(&rhs[0])
117+
});
118+
119+
// Push the first end time to the heap
120+
pq.push(Reverse(intervals[0][1]));
121+
122+
// Traverse the intervals vector
123+
for i in 1..n {
124+
// Get the current top element from the heap
125+
if let Some(Reverse(end_time)) = pq.pop() {
126+
if end_time <= intervals[i][0] {
127+
// If the end time is early than the current begin time
128+
let new_end_time = intervals[i][1];
129+
pq.push(Reverse(new_end_time));
130+
} else {
131+
// Otherwise, push the end time back and we also need a new room
132+
pq.push(Reverse(end_time));
133+
pq.push(Reverse(intervals[i][1]));
134+
}
135+
}
136+
}
137+
138+
pq.len() as i32
139+
}
140+
}
141+
```
142+
101143
### **Go**
102144

103145
```go

solution/0200-0299/0253.Meeting Rooms II/README_EN.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,48 @@ public:
7979
};
8080
```
8181
82+
### **Rust**
83+
84+
```rust
85+
use std::{collections::BinaryHeap, cmp::Reverse};
86+
87+
impl Solution {
88+
#[allow(dead_code)]
89+
pub fn min_meeting_rooms(intervals: Vec<Vec<i32>>) -> i32 {
90+
// The min heap that stores the earliest ending time among all meeting rooms
91+
let mut pq = BinaryHeap::new();
92+
let mut intervals = intervals;
93+
let n = intervals.len();
94+
95+
// Let's first sort the intervals vector
96+
intervals.sort_by(|lhs, rhs| {
97+
lhs[0].cmp(&rhs[0])
98+
});
99+
100+
// Push the first end time to the heap
101+
pq.push(Reverse(intervals[0][1]));
102+
103+
// Traverse the intervals vector
104+
for i in 1..n {
105+
// Get the current top element from the heap
106+
if let Some(Reverse(end_time)) = pq.pop() {
107+
if end_time <= intervals[i][0] {
108+
// If the end time is early than the current begin time
109+
let new_end_time = intervals[i][1];
110+
pq.push(Reverse(new_end_time));
111+
} else {
112+
// Otherwise, push the end time back and we also need a new room
113+
pq.push(Reverse(end_time));
114+
pq.push(Reverse(intervals[i][1]));
115+
}
116+
}
117+
}
118+
119+
pq.len() as i32
120+
}
121+
}
122+
```
123+
82124
### **Go**
83125

84126
```go
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use std::{collections::BinaryHeap, cmp::Reverse};
2+
3+
impl Solution {
4+
#[allow(dead_code)]
5+
pub fn min_meeting_rooms(intervals: Vec<Vec<i32>>) -> i32 {
6+
// The min heap that stores the earliest ending time among all meeting rooms
7+
let mut pq = BinaryHeap::new();
8+
let mut intervals = intervals;
9+
let n = intervals.len();
10+
11+
// Let's first sort the intervals vector
12+
intervals.sort_by(|lhs, rhs| {
13+
lhs[0].cmp(&rhs[0])
14+
});
15+
16+
// Push the first end time to the heap
17+
pq.push(Reverse(intervals[0][1]));
18+
19+
// Traverse the intervals vector
20+
for i in 1..n {
21+
// Get the current top element from the heap
22+
if let Some(Reverse(end_time)) = pq.pop() {
23+
if end_time <= intervals[i][0] {
24+
// If the end time is early than the current begin time
25+
let new_end_time = intervals[i][1];
26+
pq.push(Reverse(new_end_time));
27+
} else {
28+
// Otherwise, push the end time back and we also need a new room
29+
pq.push(Reverse(end_time));
30+
pq.push(Reverse(intervals[i][1]));
31+
}
32+
}
33+
}
34+
35+
pq.len() as i32
36+
}
37+
}

0 commit comments

Comments
 (0)