Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions solution/2700-2799/2751.Robot Collisions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,56 @@ function survivedRobotsHealths(
}
```

#### Rust

```rust
impl Solution {
pub fn survived_robots_healths(
positions: Vec<i32>,
mut healths: Vec<i32>,
directions: String
) -> Vec<i32> {
let n = positions.len();
let mut idx: Vec<usize> = (0..n).collect();

idx.sort_by_key(|&i| positions[i]);

let dirs = directions.as_bytes();
let mut stk: Vec<usize> = Vec::new();

for &i in &idx {
if dirs[i] == b'R' {
stk.push(i);
continue;
}

while let Some(&j) = stk.last() {
if healths[i] == 0 {
break;
}

if healths[j] > healths[i] {
healths[j] -= 1;
healths[i] = 0;
break;
} else if healths[j] < healths[i] {
healths[i] -= 1;
healths[j] = 0;
stk.pop();
} else {
healths[i] = 0;
healths[j] = 0;
stk.pop();
break;
}
}
}

healths.into_iter().filter(|&h| h > 0).collect()
}
}
```

#### JavaScript

```js
Expand Down
50 changes: 50 additions & 0 deletions solution/2700-2799/2751.Robot Collisions/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,56 @@ function survivedRobotsHealths(
}
```

#### Rust

```rust
impl Solution {
pub fn survived_robots_healths(
positions: Vec<i32>,
mut healths: Vec<i32>,
directions: String
) -> Vec<i32> {
let n = positions.len();
let mut idx: Vec<usize> = (0..n).collect();

idx.sort_by_key(|&i| positions[i]);

let dirs = directions.as_bytes();
let mut stk: Vec<usize> = Vec::new();

for &i in &idx {
if dirs[i] == b'R' {
stk.push(i);
continue;
}

while let Some(&j) = stk.last() {
if healths[i] == 0 {
break;
}

if healths[j] > healths[i] {
healths[j] -= 1;
healths[i] = 0;
break;
} else if healths[j] < healths[i] {
healths[i] -= 1;
healths[j] = 0;
stk.pop();
} else {
healths[i] = 0;
healths[j] = 0;
stk.pop();
break;
}
}
}

healths.into_iter().filter(|&h| h > 0).collect()
}
}
```

#### JavaScript

```js
Expand Down
45 changes: 45 additions & 0 deletions solution/2700-2799/2751.Robot Collisions/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
impl Solution {
pub fn survived_robots_healths(
positions: Vec<i32>,
mut healths: Vec<i32>,
directions: String
) -> Vec<i32> {
let n = positions.len();
let mut idx: Vec<usize> = (0..n).collect();

idx.sort_by_key(|&i| positions[i]);

let dirs = directions.as_bytes();
let mut stk: Vec<usize> = Vec::new();

for &i in &idx {
if dirs[i] == b'R' {
stk.push(i);
continue;
}

while let Some(&j) = stk.last() {
if healths[i] == 0 {
break;
}

if healths[j] > healths[i] {
healths[j] -= 1;
healths[i] = 0;
break;
} else if healths[j] < healths[i] {
healths[i] -= 1;
healths[j] = 0;
stk.pop();
} else {
healths[i] = 0;
healths[j] = 0;
stk.pop();
break;
}
}
}

healths.into_iter().filter(|&h| h > 0).collect()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/2700-2799/2753.Co
<pre>
<strong>Input:</strong> street = [1,1,1,1], k = 10
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are 4 houses, and all their doors are open.
<strong>Explanation:</strong> There are 4 houses, and all their doors are open.
The number of houses is less than k, which is 10.</pre>

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

<!-- solution:start -->

### Solution 1
### Solution 1: Brain Teaser

We notice that there is at least one door open in the problem. We can first find one of the open doors.

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.

The time complexity is $O(k)$, and the space complexity is $O(1)$.

Related problem:

- [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)

<!-- tabs:start -->

Expand Down
Loading