Skip to content

Commit f8ed7c6

Browse files
authored
feat: add rust solution to lc problem: No.0218 (#1909)
No.0218.The Skyline Problem
1 parent c1725f2 commit f8ed7c6

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

solution/0200-0299/0218.The Skyline Problem/README.md

+37
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,43 @@ public:
184184
};
185185
```
186186
187+
### **Rust**
188+
189+
```rust
190+
impl Solution {
191+
pub fn get_skyline(buildings: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
192+
let mut skys: Vec<Vec<i32>> = vec![];
193+
let mut lines = vec![];
194+
for building in buildings.iter() {
195+
lines.push(building[0]);
196+
lines.push(building[1]);
197+
}
198+
lines.sort_unstable();
199+
let mut pq = std::collections::BinaryHeap::new();
200+
let (mut city, n) = (0, buildings.len());
201+
202+
for line in lines {
203+
while city < n && buildings[city][0] <= line && buildings[city][1] > line {
204+
pq.push((buildings[city][2], buildings[city][1]));
205+
city += 1;
206+
}
207+
while !pq.is_empty() && pq.peek().unwrap().1 <= line {
208+
pq.pop();
209+
}
210+
let mut high = 0;
211+
if !pq.is_empty() {
212+
high = pq.peek().unwrap().0;
213+
}
214+
if !skys.is_empty() && skys.last().unwrap()[1] == high {
215+
continue;
216+
}
217+
skys.push(vec![line, high]);
218+
}
219+
skys
220+
}
221+
}
222+
```
223+
187224
### **...**
188225

189226
```

solution/0200-0299/0218.The Skyline Problem/README_EN.md

+37
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,43 @@ public:
171171
};
172172
```
173173
174+
### **Rust**
175+
176+
```rust
177+
impl Solution {
178+
pub fn get_skyline(buildings: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
179+
let mut skys: Vec<Vec<i32>> = vec![];
180+
let mut lines = vec![];
181+
for building in buildings.iter() {
182+
lines.push(building[0]);
183+
lines.push(building[1]);
184+
}
185+
lines.sort_unstable();
186+
let mut pq = std::collections::BinaryHeap::new();
187+
let (mut city, n) = (0, buildings.len());
188+
189+
for line in lines {
190+
while city < n && buildings[city][0] <= line && buildings[city][1] > line {
191+
pq.push((buildings[city][2], buildings[city][1]));
192+
city += 1;
193+
}
194+
while !pq.is_empty() && pq.peek().unwrap().1 <= line {
195+
pq.pop();
196+
}
197+
let mut high = 0;
198+
if !pq.is_empty() {
199+
high = pq.peek().unwrap().0;
200+
}
201+
if !skys.is_empty() && skys.last().unwrap()[1] == high {
202+
continue;
203+
}
204+
skys.push(vec![line, high]);
205+
}
206+
skys
207+
}
208+
}
209+
```
210+
174211
### **...**
175212

176213
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
impl Solution {
2+
pub fn get_skyline(buildings: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
3+
let mut skys: Vec<Vec<i32>> = vec![];
4+
let mut lines = vec![];
5+
for building in buildings.iter() {
6+
lines.push(building[0]);
7+
lines.push(building[1]);
8+
}
9+
lines.sort_unstable();
10+
let mut pq = std::collections::BinaryHeap::new();
11+
let (mut city, n) = (0, buildings.len());
12+
13+
for line in lines {
14+
while city < n && buildings[city][0] <= line && buildings[city][1] > line {
15+
pq.push((buildings[city][2], buildings[city][1]));
16+
city += 1;
17+
}
18+
while !pq.is_empty() && pq.peek().unwrap().1 <= line {
19+
pq.pop();
20+
}
21+
let mut high = 0;
22+
if !pq.is_empty() {
23+
high = pq.peek().unwrap().0;
24+
}
25+
if !skys.is_empty() && skys.last().unwrap()[1] == high {
26+
continue;
27+
}
28+
skys.push(vec![line, high]);
29+
}
30+
skys
31+
}
32+
}

0 commit comments

Comments
 (0)