Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ff4e0d3

Browse files
authored
feat: add solutions to lc problem: No.2534 (doocs#3723)
No.2534.Time Taken to Cross the Door
1 parent dc4d031 commit ff4e0d3

File tree

9 files changed

+569
-9
lines changed

9 files changed

+569
-9
lines changed

solution/2500-2599/2532.Time to Cross a Bridge/README_EN.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,30 @@ From 49 to 50: worker 0 crosses the bridge to the left.
107107

108108
<!-- solution:start -->
109109

110-
### Solution 1
110+
### Solution 1: Priority Queue (Max-Heap and Min-Heap) + Simulation
111+
112+
First, we sort the workers by efficiency in descending order, so the worker with the highest index has the lowest efficiency.
113+
114+
Next, we use four priority queues to simulate the state of the workers:
115+
116+
- `wait_in_left`: Max-heap, storing the indices of workers currently waiting on the left bank;
117+
- `wait_in_right`: Max-heap, storing the indices of workers currently waiting on the right bank;
118+
- `work_in_left`: Min-heap, storing the time when workers currently working on the left bank finish placing boxes and the indices of the workers;
119+
- `work_in_right`: Min-heap, storing the time when workers currently working on the right bank finish picking up boxes and the indices of the workers.
120+
121+
Initially, all workers are on the left bank, so `wait_in_left` stores the indices of all workers. We use the variable `cur` to record the current time.
122+
123+
Then, we simulate the entire process. First, we check if any worker in `work_in_left` has finished placing boxes at the current time. If so, we move the worker to `wait_in_left` and remove the worker from `work_in_left`. Similarly, we check if any worker in `work_in_right` has finished picking up boxes. If so, we move the worker to `wait_in_right` and remove the worker from `work_in_right`.
124+
125+
Next, we check if there are any workers waiting on the left bank at the current time, denoted as `left_to_go`. At the same time, we check if there are any workers waiting on the right bank, denoted as `right_to_go`. If there are no workers waiting to cross the river, we directly update `cur` to the next time when a worker finishes placing boxes and continue the simulation.
126+
127+
If `right_to_go` is `true`, we take a worker from `wait_in_right`, update `cur` to the current time plus the time it takes for the worker to cross from the right bank to the left bank. If all workers have crossed to the right bank at this point, we directly return `cur` as the answer; otherwise, we move the worker to `work_in_left`.
128+
129+
If `left_to_go` is `true`, we take a worker from `wait_in_left`, update `cur` to the current time plus the time it takes for the worker to cross from the left bank to the right bank, then move the worker to `work_in_right` and decrement the number of boxes.
130+
131+
Repeat the above process until the number of boxes is zero. At this point, `cur` is the answer.
132+
133+
The time complexity is $O(n \times \log k)$, and the space complexity is $O(k)$. Here, $n$ and $k$ are the number of workers and the number of boxes, respectively.
111134

112135
<!-- tabs:start -->
113136

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

Lines changed: 189 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,32 +83,217 @@ 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

90102
#### Python3
91103

92104
```python
93-
105+
class Solution:
106+
def timeTaken(self, arrival: List[int], state: List[int]) -> List[int]:
107+
q = [deque(), deque()]
108+
n = len(arrival)
109+
t = i = 0
110+
st = 1
111+
ans = [0] * n
112+
while i < n or q[0] or q[1]:
113+
while i < n and arrival[i] <= t:
114+
q[state[i]].append(i)
115+
i += 1
116+
if q[0] and q[1]:
117+
ans[q[st].popleft()] = t
118+
elif q[0] or q[1]:
119+
st = 0 if q[0] else 1
120+
ans[q[st].popleft()] = t
121+
else:
122+
st = 1
123+
t += 1
124+
return ans
94125
```
95126

96127
#### Java
97128

98129
```java
99-
130+
class Solution {
131+
public int[] timeTaken(int[] arrival, int[] state) {
132+
Deque<Integer>[] q = new Deque[2];
133+
Arrays.setAll(q, i -> new ArrayDeque<>());
134+
int n = arrival.length;
135+
int t = 0, i = 0, st = 1;
136+
int[] ans = new int[n];
137+
while (i < n || !q[0].isEmpty() || !q[1].isEmpty()) {
138+
while (i < n && arrival[i] <= t) {
139+
q[state[i]].add(i++);
140+
}
141+
if (!q[0].isEmpty() && !q[1].isEmpty()) {
142+
ans[q[st].poll()] = t;
143+
} else if (!q[0].isEmpty() || !q[1].isEmpty()) {
144+
st = q[0].isEmpty() ? 1 : 0;
145+
ans[q[st].poll()] = t;
146+
} else {
147+
st = 1;
148+
}
149+
++t;
150+
}
151+
return ans;
152+
}
153+
}
100154
```
101155

102156
#### C++
103157

104158
```cpp
105-
159+
class Solution {
160+
public:
161+
vector<int> timeTaken(vector<int>& arrival, vector<int>& state) {
162+
int n = arrival.size();
163+
queue<int> q[2];
164+
int t = 0, i = 0, st = 1;
165+
vector<int> ans(n);
166+
167+
while (i < n || !q[0].empty() || !q[1].empty()) {
168+
while (i < n && arrival[i] <= t) {
169+
q[state[i]].push(i++);
170+
}
171+
172+
if (!q[0].empty() && !q[1].empty()) {
173+
ans[q[st].front()] = t;
174+
q[st].pop();
175+
} else if (!q[0].empty() || !q[1].empty()) {
176+
st = q[0].empty() ? 1 : 0;
177+
ans[q[st].front()] = t;
178+
q[st].pop();
179+
} else {
180+
st = 1;
181+
}
182+
183+
++t;
184+
}
185+
186+
return ans;
187+
}
188+
};
106189
```
107190

108191
#### Go
109192

110193
```go
194+
func timeTaken(arrival []int, state []int) []int {
195+
n := len(arrival)
196+
q := [2][]int{}
197+
t, i, st := 0, 0, 1
198+
ans := make([]int, n)
199+
200+
for i < n || len(q[0]) > 0 || len(q[1]) > 0 {
201+
for i < n && arrival[i] <= t {
202+
q[state[i]] = append(q[state[i]], i)
203+
i++
204+
}
205+
206+
if len(q[0]) > 0 && len(q[1]) > 0 {
207+
ans[q[st][0]] = t
208+
q[st] = q[st][1:]
209+
} else if len(q[0]) > 0 || len(q[1]) > 0 {
210+
if len(q[0]) == 0 {
211+
st = 1
212+
} else {
213+
st = 0
214+
}
215+
ans[q[st][0]] = t
216+
q[st] = q[st][1:]
217+
} else {
218+
st = 1
219+
}
220+
221+
t++
222+
}
223+
224+
return ans
225+
}
226+
```
227+
228+
#### TypeScript
229+
230+
```ts
231+
function timeTaken(arrival: number[], state: number[]): number[] {
232+
const n = arrival.length;
233+
const q: number[][] = [[], []];
234+
let [t, i, st] = [0, 0, 1];
235+
const ans: number[] = Array(n).fill(0);
236+
237+
while (i < n || q[0].length || q[1].length) {
238+
while (i < n && arrival[i] <= t) {
239+
q[state[i]].push(i++);
240+
}
241+
242+
if (q[0].length && q[1].length) {
243+
ans[q[st][0]] = t;
244+
q[st].shift();
245+
} else if (q[0].length || q[1].length) {
246+
st = q[0].length ? 0 : 1;
247+
ans[q[st][0]] = t;
248+
q[st].shift();
249+
} else {
250+
st = 1;
251+
}
252+
253+
t++;
254+
}
255+
256+
return ans;
257+
}
258+
```
111259

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+
}
112297
```
113298

114299
<!-- tabs:end -->

0 commit comments

Comments
 (0)