Skip to content

Commit 62008e5

Browse files
authored
feat: add rust solution to lc problem: No.3531 (#4896)
1 parent 2fa413e commit 62008e5

File tree

4 files changed

+260
-0
lines changed

4 files changed

+260
-0
lines changed

solution/3500-3599/3531.Count Covered Buildings/README.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,96 @@ function countCoveredBuildings(n: number, buildings: number[][]): number {
297297
}
298298
```
299299

300+
#### Rust
301+
302+
```rust
303+
use std::collections::HashMap;
304+
305+
impl Solution {
306+
pub fn count_covered_buildings(_n: i32, buildings: Vec<Vec<i32>>) -> i32 {
307+
let mut g1: HashMap<i32, Vec<i32>> = HashMap::new();
308+
let mut g2: HashMap<i32, Vec<i32>> = HashMap::new();
309+
310+
for b in &buildings {
311+
let x = b[0];
312+
let y = b[1];
313+
g1.entry(x).or_insert(Vec::new()).push(y);
314+
g2.entry(y).or_insert(Vec::new()).push(x);
315+
}
316+
317+
for v in g1.values_mut() {
318+
v.sort();
319+
}
320+
for v in g2.values_mut() {
321+
v.sort();
322+
}
323+
324+
let mut ans: i32 = 0;
325+
326+
for b in &buildings {
327+
let x = b[0];
328+
let y = b[1];
329+
330+
let l1 = g1.get(&x).unwrap();
331+
let l2 = g2.get(&y).unwrap();
332+
333+
if l2[0] < x && x < l2[l2.len() - 1] && l1[0] < y && y < l1[l1.len() - 1] {
334+
ans += 1;
335+
}
336+
}
337+
338+
ans
339+
}
340+
}
341+
```
342+
343+
#### C#
344+
345+
```cs
346+
public class Solution {
347+
public int CountCoveredBuildings(int n, int[][] buildings) {
348+
var g1 = new Dictionary<int, List<int>>();
349+
var g2 = new Dictionary<int, List<int>>();
350+
351+
foreach (var b in buildings) {
352+
int x = b[0], y = b[1];
353+
354+
if (!g1.ContainsKey(x)) {
355+
g1[x] = new List<int>();
356+
}
357+
g1[x].Add(y);
358+
359+
if (!g2.ContainsKey(y)) {
360+
g2[y] = new List<int>();
361+
}
362+
g2[y].Add(x);
363+
}
364+
365+
foreach (var kv in g1) {
366+
kv.Value.Sort();
367+
}
368+
foreach (var kv in g2) {
369+
kv.Value.Sort();
370+
}
371+
372+
int ans = 0;
373+
374+
foreach (var b in buildings) {
375+
int x = b[0], y = b[1];
376+
var l1 = g1[x];
377+
var l2 = g2[y];
378+
379+
if (l2[0] < x && x < l2[l2.Count - 1] &&
380+
l1[0] < y && y < l1[l1.Count - 1]) {
381+
ans++;
382+
}
383+
}
384+
385+
return ans;
386+
}
387+
}
388+
```
389+
300390
<!-- tabs:end -->
301391

302392
<!-- solution:end -->

solution/3500-3599/3531.Count Covered Buildings/README_EN.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,96 @@ function countCoveredBuildings(n: number, buildings: number[][]): number {
295295
}
296296
```
297297

298+
#### Rust
299+
300+
```rust
301+
use std::collections::HashMap;
302+
303+
impl Solution {
304+
pub fn count_covered_buildings(_n: i32, buildings: Vec<Vec<i32>>) -> i32 {
305+
let mut g1: HashMap<i32, Vec<i32>> = HashMap::new();
306+
let mut g2: HashMap<i32, Vec<i32>> = HashMap::new();
307+
308+
for b in &buildings {
309+
let x = b[0];
310+
let y = b[1];
311+
g1.entry(x).or_insert(Vec::new()).push(y);
312+
g2.entry(y).or_insert(Vec::new()).push(x);
313+
}
314+
315+
for v in g1.values_mut() {
316+
v.sort();
317+
}
318+
for v in g2.values_mut() {
319+
v.sort();
320+
}
321+
322+
let mut ans: i32 = 0;
323+
324+
for b in &buildings {
325+
let x = b[0];
326+
let y = b[1];
327+
328+
let l1 = g1.get(&x).unwrap();
329+
let l2 = g2.get(&y).unwrap();
330+
331+
if l2[0] < x && x < l2[l2.len() - 1] && l1[0] < y && y < l1[l1.len() - 1] {
332+
ans += 1;
333+
}
334+
}
335+
336+
ans
337+
}
338+
}
339+
```
340+
341+
#### C#
342+
343+
```cs
344+
public class Solution {
345+
public int CountCoveredBuildings(int n, int[][] buildings) {
346+
var g1 = new Dictionary<int, List<int>>();
347+
var g2 = new Dictionary<int, List<int>>();
348+
349+
foreach (var b in buildings) {
350+
int x = b[0], y = b[1];
351+
352+
if (!g1.ContainsKey(x)) {
353+
g1[x] = new List<int>();
354+
}
355+
g1[x].Add(y);
356+
357+
if (!g2.ContainsKey(y)) {
358+
g2[y] = new List<int>();
359+
}
360+
g2[y].Add(x);
361+
}
362+
363+
foreach (var kv in g1) {
364+
kv.Value.Sort();
365+
}
366+
foreach (var kv in g2) {
367+
kv.Value.Sort();
368+
}
369+
370+
int ans = 0;
371+
372+
foreach (var b in buildings) {
373+
int x = b[0], y = b[1];
374+
var l1 = g1[x];
375+
var l2 = g2[y];
376+
377+
if (l2[0] < x && x < l2[l2.Count - 1] &&
378+
l1[0] < y && y < l1[l1.Count - 1]) {
379+
ans++;
380+
}
381+
}
382+
383+
return ans;
384+
}
385+
}
386+
```
387+
298388
<!-- tabs:end -->
299389

300390
<!-- solution:end -->
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
public class Solution {
2+
public int CountCoveredBuildings(int n, int[][] buildings) {
3+
var g1 = new Dictionary<int, List<int>>();
4+
var g2 = new Dictionary<int, List<int>>();
5+
6+
foreach (var b in buildings) {
7+
int x = b[0], y = b[1];
8+
9+
if (!g1.ContainsKey(x)) {
10+
g1[x] = new List<int>();
11+
}
12+
g1[x].Add(y);
13+
14+
if (!g2.ContainsKey(y)) {
15+
g2[y] = new List<int>();
16+
}
17+
g2[y].Add(x);
18+
}
19+
20+
foreach (var kv in g1) {
21+
kv.Value.Sort();
22+
}
23+
foreach (var kv in g2) {
24+
kv.Value.Sort();
25+
}
26+
27+
int ans = 0;
28+
29+
foreach (var b in buildings) {
30+
int x = b[0], y = b[1];
31+
var l1 = g1[x];
32+
var l2 = g2[y];
33+
34+
if (l2[0] < x && x < l2[l2.Count - 1] &&
35+
l1[0] < y && y < l1[l1.Count - 1]) {
36+
ans++;
37+
}
38+
}
39+
40+
return ans;
41+
}
42+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use std::collections::HashMap;
2+
3+
impl Solution {
4+
pub fn count_covered_buildings(_n: i32, buildings: Vec<Vec<i32>>) -> i32 {
5+
let mut g1: HashMap<i32, Vec<i32>> = HashMap::new();
6+
let mut g2: HashMap<i32, Vec<i32>> = HashMap::new();
7+
8+
for b in &buildings {
9+
let x = b[0];
10+
let y = b[1];
11+
g1.entry(x).or_insert(Vec::new()).push(y);
12+
g2.entry(y).or_insert(Vec::new()).push(x);
13+
}
14+
15+
for v in g1.values_mut() {
16+
v.sort();
17+
}
18+
for v in g2.values_mut() {
19+
v.sort();
20+
}
21+
22+
let mut ans: i32 = 0;
23+
24+
for b in &buildings {
25+
let x = b[0];
26+
let y = b[1];
27+
28+
let l1 = g1.get(&x).unwrap();
29+
let l2 = g2.get(&y).unwrap();
30+
31+
if l2[0] < x && x < l2[l2.len() - 1] && l1[0] < y && y < l1[l1.len() - 1] {
32+
ans += 1;
33+
}
34+
}
35+
36+
ans
37+
}
38+
}

0 commit comments

Comments
 (0)