File tree 3 files changed +106
-0
lines changed
solution/0200-0299/0218.The Skyline Problem
3 files changed +106
-0
lines changed Original file line number Diff line number Diff line change @@ -184,6 +184,43 @@ public:
184
184
};
185
185
```
186
186
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
+
187
224
### ** ...**
188
225
189
226
```
Original file line number Diff line number Diff line change @@ -171,6 +171,43 @@ public:
171
171
};
172
172
```
173
173
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
+
174
211
### ** ...**
175
212
176
213
```
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments