Skip to content

Commit 869c77a

Browse files
committed
feat: add solutions to lc problem: No.2534
No.2534.Time Taken to Cross the Door
1 parent 0bb62e0 commit 869c77a

File tree

3 files changed

+138
-2
lines changed

3 files changed

+138
-2
lines changed

solution/2500-2599/2534.Time Taken to Cross the Door/README.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,19 @@ tags:
8383

8484
<!-- solution:start -->
8585

86-
### 方法一
86+
### 方法一:队列 + 模拟
87+
88+
我们定义两个队列,其中 $q[0]$ 存放想要进入的人的编号,而 $q[1]$ 存放想要离开的人的编号。
89+
90+
我们维护一个时间 $t$,表示当前时间,一个状态 $st$,表示当前门的状态,当 $st = 1$ 表示门没使用或者上一秒有人离开,当 $st = 0$ 表示上一秒有人进入。初始时 $t = 0$,而 $st = 1$。
91+
92+
我们遍历数组 $\textit{arrival}$,对于每个人,如果当前时间 $t$ 小于等于该人到达门前的时间 $arrival[i]$,我们将该人的编号加入对应的队列 $q[\text{state}[i]]$ 中。
93+
94+
然后我们判断当前队列 $q[0]$ 和 $q[1]$ 是否都不为空,如果都不为空,我们将 $q[st]$ 队列的队首元素出队,并将当前时间 $t$ 赋值给该人的通过时间;如果只有一个队列不为空,我们根据哪个队列不为空,更新 $st$ 的值,然后将该队列的队首元素出队,并将当前时间 $t$ 赋值给该人的通过时间;如果两个队列都为空,我们将 $st$ 的值更新为 $1$,表示门没使用。
95+
96+
接下来,我们将时间 $t$ 自增 $1$,继续遍历数组 $\textit{arrival}$,直到所有人都通过门。
97+
98+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 表示数组 $\textit{arrival}$ 的长度。
8799

88100
<!-- tabs:start -->
89101

@@ -245,6 +257,45 @@ function timeTaken(arrival: number[], state: number[]): number[] {
245257
}
246258
```
247259

260+
#### Rust
261+
262+
```rust
263+
use std::collections::VecDeque;
264+
265+
impl Solution {
266+
pub fn time_taken(arrival: Vec<i32>, state: Vec<i32>) -> Vec<i32> {
267+
let n = arrival.len();
268+
let mut q = vec![VecDeque::new(), VecDeque::new()];
269+
let mut t = 0;
270+
let mut i = 0;
271+
let mut st = 1;
272+
let mut ans = vec![-1; n];
273+
274+
while i < n || !q[0].is_empty() || !q[1].is_empty() {
275+
while i < n && arrival[i] <= t {
276+
q[state[i] as usize].push_back(i);
277+
i += 1;
278+
}
279+
280+
if !q[0].is_empty() && !q[1].is_empty() {
281+
ans[*q[st].front().unwrap()] = t;
282+
q[st].pop_front();
283+
} else if !q[0].is_empty() || !q[1].is_empty() {
284+
st = if q[0].is_empty() { 1 } else { 0 };
285+
ans[*q[st].front().unwrap()] = t;
286+
q[st].pop_front();
287+
} else {
288+
st = 1;
289+
}
290+
291+
t += 1;
292+
}
293+
294+
ans
295+
}
296+
}
297+
```
298+
248299
<!-- tabs:end -->
249300

250301
<!-- solution:end -->

solution/2500-2599/2534.Time Taken to Cross the Door/README_EN.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,19 @@ tags:
8282

8383
<!-- solution:start -->
8484

85-
### Solution 1
85+
### Solution 1: Queue + Simulation
86+
87+
We define two queues, where $q[0]$ stores the indices of people who want to enter, and $q[1]$ stores the indices of people who want to exit.
88+
89+
We maintain a variable $t$ to represent the current time, and a variable $st$ to represent the current state of the door. When $st = 1$, it means the door is not in use or someone exited in the previous second. When $st = 0$, it means someone entered in the previous second. Initially, $t = 0$ and $st = 1$.
90+
91+
We traverse the array $\textit{arrival}$. For each person, if the current time $t$ is less than or equal to the time the person arrives at the door $\textit{arrival}[i]$, we add the person's index to the corresponding queue $q[\text{state}[i]]$.
92+
93+
Then we check if both queues $q[0]$ and $q[1]$ are not empty. If both are not empty, we dequeue the front element from the queue $q[st]$ and assign the current time $t$ to the person's passing time. If only one queue is not empty, we update the value of $st$ based on which queue is not empty, then dequeue the front element from that queue and assign the current time $t$ to the person's passing time. If both queues are empty, we update the value of $st$ to $1$, indicating the door is not in use.
94+
95+
Next, we increment the time $t$ by $1$ and continue traversing the array $\textit{arrival}$ until all people have passed through the door.
96+
97+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ represents the length of the array $\textit{arrival}$.
8698

8799
<!-- tabs:start -->
88100

@@ -244,6 +256,45 @@ function timeTaken(arrival: number[], state: number[]): number[] {
244256
}
245257
```
246258

259+
#### Rust
260+
261+
```rust
262+
use std::collections::VecDeque;
263+
264+
impl Solution {
265+
pub fn time_taken(arrival: Vec<i32>, state: Vec<i32>) -> Vec<i32> {
266+
let n = arrival.len();
267+
let mut q = vec![VecDeque::new(), VecDeque::new()];
268+
let mut t = 0;
269+
let mut i = 0;
270+
let mut st = 1;
271+
let mut ans = vec![-1; n];
272+
273+
while i < n || !q[0].is_empty() || !q[1].is_empty() {
274+
while i < n && arrival[i] <= t {
275+
q[state[i] as usize].push_back(i);
276+
i += 1;
277+
}
278+
279+
if !q[0].is_empty() && !q[1].is_empty() {
280+
ans[*q[st].front().unwrap()] = t;
281+
q[st].pop_front();
282+
} else if !q[0].is_empty() || !q[1].is_empty() {
283+
st = if q[0].is_empty() { 1 } else { 0 };
284+
ans[*q[st].front().unwrap()] = t;
285+
q[st].pop_front();
286+
} else {
287+
st = 1;
288+
}
289+
290+
t += 1;
291+
}
292+
293+
ans
294+
}
295+
}
296+
```
297+
247298
<!-- tabs:end -->
248299

249300
<!-- solution:end -->
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use std::collections::VecDeque;
2+
3+
impl Solution {
4+
pub fn time_taken(arrival: Vec<i32>, state: Vec<i32>) -> Vec<i32> {
5+
let n = arrival.len();
6+
let mut q = vec![VecDeque::new(), VecDeque::new()];
7+
let mut t = 0;
8+
let mut i = 0;
9+
let mut st = 1;
10+
let mut ans = vec![-1; n];
11+
12+
while i < n || !q[0].is_empty() || !q[1].is_empty() {
13+
while i < n && arrival[i] <= t {
14+
q[state[i] as usize].push_back(i);
15+
i += 1;
16+
}
17+
18+
if !q[0].is_empty() && !q[1].is_empty() {
19+
ans[*q[st].front().unwrap()] = t;
20+
q[st].pop_front();
21+
} else if !q[0].is_empty() || !q[1].is_empty() {
22+
st = if q[0].is_empty() { 1 } else { 0 };
23+
ans[*q[st].front().unwrap()] = t;
24+
q[st].pop_front();
25+
} else {
26+
st = 1;
27+
}
28+
29+
t += 1;
30+
}
31+
32+
ans
33+
}
34+
}

0 commit comments

Comments
 (0)