Skip to content

Commit f9a020e

Browse files
authored
feat: add Rust solution for lc No.2751 (#5127)
1 parent 464421f commit f9a020e

4 files changed

Lines changed: 157 additions & 2 deletions

File tree

solution/2700-2799/2751.Robot Collisions/README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,56 @@ function survivedRobotsHealths(
318318
}
319319
```
320320

321+
#### Rust
322+
323+
```rust
324+
impl Solution {
325+
pub fn survived_robots_healths(
326+
positions: Vec<i32>,
327+
mut healths: Vec<i32>,
328+
directions: String
329+
) -> Vec<i32> {
330+
let n = positions.len();
331+
let mut idx: Vec<usize> = (0..n).collect();
332+
333+
idx.sort_by_key(|&i| positions[i]);
334+
335+
let dirs = directions.as_bytes();
336+
let mut stk: Vec<usize> = Vec::new();
337+
338+
for &i in &idx {
339+
if dirs[i] == b'R' {
340+
stk.push(i);
341+
continue;
342+
}
343+
344+
while let Some(&j) = stk.last() {
345+
if healths[i] == 0 {
346+
break;
347+
}
348+
349+
if healths[j] > healths[i] {
350+
healths[j] -= 1;
351+
healths[i] = 0;
352+
break;
353+
} else if healths[j] < healths[i] {
354+
healths[i] -= 1;
355+
healths[j] = 0;
356+
stk.pop();
357+
} else {
358+
healths[i] = 0;
359+
healths[j] = 0;
360+
stk.pop();
361+
break;
362+
}
363+
}
364+
}
365+
366+
healths.into_iter().filter(|&h| h > 0).collect()
367+
}
368+
}
369+
```
370+
321371
#### JavaScript
322372

323373
```js

solution/2700-2799/2751.Robot Collisions/README_EN.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,56 @@ function survivedRobotsHealths(
318318
}
319319
```
320320

321+
#### Rust
322+
323+
```rust
324+
impl Solution {
325+
pub fn survived_robots_healths(
326+
positions: Vec<i32>,
327+
mut healths: Vec<i32>,
328+
directions: String
329+
) -> Vec<i32> {
330+
let n = positions.len();
331+
let mut idx: Vec<usize> = (0..n).collect();
332+
333+
idx.sort_by_key(|&i| positions[i]);
334+
335+
let dirs = directions.as_bytes();
336+
let mut stk: Vec<usize> = Vec::new();
337+
338+
for &i in &idx {
339+
if dirs[i] == b'R' {
340+
stk.push(i);
341+
continue;
342+
}
343+
344+
while let Some(&j) = stk.last() {
345+
if healths[i] == 0 {
346+
break;
347+
}
348+
349+
if healths[j] > healths[i] {
350+
healths[j] -= 1;
351+
healths[i] = 0;
352+
break;
353+
} else if healths[j] < healths[i] {
354+
healths[i] -= 1;
355+
healths[j] = 0;
356+
stk.pop();
357+
} else {
358+
healths[i] = 0;
359+
healths[j] = 0;
360+
stk.pop();
361+
break;
362+
}
363+
}
364+
}
365+
366+
healths.into_iter().filter(|&h| h > 0).collect()
367+
}
368+
}
369+
```
370+
321371
#### JavaScript
322372

323373
```js
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
impl Solution {
2+
pub fn survived_robots_healths(
3+
positions: Vec<i32>,
4+
mut healths: Vec<i32>,
5+
directions: String
6+
) -> Vec<i32> {
7+
let n = positions.len();
8+
let mut idx: Vec<usize> = (0..n).collect();
9+
10+
idx.sort_by_key(|&i| positions[i]);
11+
12+
let dirs = directions.as_bytes();
13+
let mut stk: Vec<usize> = Vec::new();
14+
15+
for &i in &idx {
16+
if dirs[i] == b'R' {
17+
stk.push(i);
18+
continue;
19+
}
20+
21+
while let Some(&j) = stk.last() {
22+
if healths[i] == 0 {
23+
break;
24+
}
25+
26+
if healths[j] > healths[i] {
27+
healths[j] -= 1;
28+
healths[i] = 0;
29+
break;
30+
} else if healths[j] < healths[i] {
31+
healths[i] -= 1;
32+
healths[j] = 0;
33+
stk.pop();
34+
} else {
35+
healths[i] = 0;
36+
healths[j] = 0;
37+
stk.pop();
38+
break;
39+
}
40+
}
41+
}
42+
43+
healths.into_iter().filter(|&h| h > 0).collect()
44+
}
45+
}

solution/2700-2799/2753.Count Houses in a Circular Street II/README_EN.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/2700-2799/2753.Co
3636
<pre>
3737
<strong>Input:</strong> street = [1,1,1,1], k = 10
3838
<strong>Output:</strong> 4
39-
<strong>Explanation:</strong> There are 4 houses, and all their doors are open.
39+
<strong>Explanation:</strong> There are 4 houses, and all their doors are open.
4040
The number of houses is less than k, which is 10.</pre>
4141

4242
<p><strong class="example">Example 2:</strong></p>
@@ -64,7 +64,17 @@ The number of houses is equal to k, which is 5.
6464

6565
<!-- solution:start -->
6666

67-
### Solution 1
67+
### Solution 1: Brain Teaser
68+
69+
We notice that there is at least one door open in the problem. We can first find one of the open doors.
70+
71+
Then, we skip this open door and move to the right. Each time we move, we increment a counter by one. If we encounter an open door, we close it. The answer is the value of the counter the last time we encounter an open door.
72+
73+
The time complexity is $O(k)$, and the space complexity is $O(1)$.
74+
75+
Related problem:
76+
77+
- [2728. Count Houses in a Circular Street](https://github.com/doocs/leetcode/blob/main/solution/2700-2799/2728.Count%20Houses%20in%20a%20Circular%20Street/README_EN.md)
6878

6979
<!-- tabs:start -->
7080

0 commit comments

Comments
 (0)