Skip to content

Commit 0bb62e0

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

File tree

8 files changed

+431
-7
lines changed

8 files changed

+431
-7
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: 137 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,25 +90,159 @@ tags:
9090
#### Python3
9191

9292
```python
93-
93+
class Solution:
94+
def timeTaken(self, arrival: List[int], state: List[int]) -> List[int]:
95+
q = [deque(), deque()]
96+
n = len(arrival)
97+
t = i = 0
98+
st = 1
99+
ans = [0] * n
100+
while i < n or q[0] or q[1]:
101+
while i < n and arrival[i] <= t:
102+
q[state[i]].append(i)
103+
i += 1
104+
if q[0] and q[1]:
105+
ans[q[st].popleft()] = t
106+
elif q[0] or q[1]:
107+
st = 0 if q[0] else 1
108+
ans[q[st].popleft()] = t
109+
else:
110+
st = 1
111+
t += 1
112+
return ans
94113
```
95114

96115
#### Java
97116

98117
```java
99-
118+
class Solution {
119+
public int[] timeTaken(int[] arrival, int[] state) {
120+
Deque<Integer>[] q = new Deque[2];
121+
Arrays.setAll(q, i -> new ArrayDeque<>());
122+
int n = arrival.length;
123+
int t = 0, i = 0, st = 1;
124+
int[] ans = new int[n];
125+
while (i < n || !q[0].isEmpty() || !q[1].isEmpty()) {
126+
while (i < n && arrival[i] <= t) {
127+
q[state[i]].add(i++);
128+
}
129+
if (!q[0].isEmpty() && !q[1].isEmpty()) {
130+
ans[q[st].poll()] = t;
131+
} else if (!q[0].isEmpty() || !q[1].isEmpty()) {
132+
st = q[0].isEmpty() ? 1 : 0;
133+
ans[q[st].poll()] = t;
134+
} else {
135+
st = 1;
136+
}
137+
++t;
138+
}
139+
return ans;
140+
}
141+
}
100142
```
101143

102144
#### C++
103145

104146
```cpp
105-
147+
class Solution {
148+
public:
149+
vector<int> timeTaken(vector<int>& arrival, vector<int>& state) {
150+
int n = arrival.size();
151+
queue<int> q[2];
152+
int t = 0, i = 0, st = 1;
153+
vector<int> ans(n);
154+
155+
while (i < n || !q[0].empty() || !q[1].empty()) {
156+
while (i < n && arrival[i] <= t) {
157+
q[state[i]].push(i++);
158+
}
159+
160+
if (!q[0].empty() && !q[1].empty()) {
161+
ans[q[st].front()] = t;
162+
q[st].pop();
163+
} else if (!q[0].empty() || !q[1].empty()) {
164+
st = q[0].empty() ? 1 : 0;
165+
ans[q[st].front()] = t;
166+
q[st].pop();
167+
} else {
168+
st = 1;
169+
}
170+
171+
++t;
172+
}
173+
174+
return ans;
175+
}
176+
};
106177
```
107178

108179
#### Go
109180

110181
```go
182+
func timeTaken(arrival []int, state []int) []int {
183+
n := len(arrival)
184+
q := [2][]int{}
185+
t, i, st := 0, 0, 1
186+
ans := make([]int, n)
187+
188+
for i < n || len(q[0]) > 0 || len(q[1]) > 0 {
189+
for i < n && arrival[i] <= t {
190+
q[state[i]] = append(q[state[i]], i)
191+
i++
192+
}
193+
194+
if len(q[0]) > 0 && len(q[1]) > 0 {
195+
ans[q[st][0]] = t
196+
q[st] = q[st][1:]
197+
} else if len(q[0]) > 0 || len(q[1]) > 0 {
198+
if len(q[0]) == 0 {
199+
st = 1
200+
} else {
201+
st = 0
202+
}
203+
ans[q[st][0]] = t
204+
q[st] = q[st][1:]
205+
} else {
206+
st = 1
207+
}
208+
209+
t++
210+
}
211+
212+
return ans
213+
}
214+
```
111215

216+
#### TypeScript
217+
218+
```ts
219+
function timeTaken(arrival: number[], state: number[]): number[] {
220+
const n = arrival.length;
221+
const q: number[][] = [[], []];
222+
let [t, i, st] = [0, 0, 1];
223+
const ans: number[] = Array(n).fill(0);
224+
225+
while (i < n || q[0].length || q[1].length) {
226+
while (i < n && arrival[i] <= t) {
227+
q[state[i]].push(i++);
228+
}
229+
230+
if (q[0].length && q[1].length) {
231+
ans[q[st][0]] = t;
232+
q[st].shift();
233+
} else if (q[0].length || q[1].length) {
234+
st = q[0].length ? 0 : 1;
235+
ans[q[st][0]] = t;
236+
q[st].shift();
237+
} else {
238+
st = 1;
239+
}
240+
241+
t++;
242+
}
243+
244+
return ans;
245+
}
112246
```
113247

114248
<!-- tabs:end -->

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

Lines changed: 137 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,25 +89,159 @@ tags:
8989
#### Python3
9090

9191
```python
92-
92+
class Solution:
93+
def timeTaken(self, arrival: List[int], state: List[int]) -> List[int]:
94+
q = [deque(), deque()]
95+
n = len(arrival)
96+
t = i = 0
97+
st = 1
98+
ans = [0] * n
99+
while i < n or q[0] or q[1]:
100+
while i < n and arrival[i] <= t:
101+
q[state[i]].append(i)
102+
i += 1
103+
if q[0] and q[1]:
104+
ans[q[st].popleft()] = t
105+
elif q[0] or q[1]:
106+
st = 0 if q[0] else 1
107+
ans[q[st].popleft()] = t
108+
else:
109+
st = 1
110+
t += 1
111+
return ans
93112
```
94113

95114
#### Java
96115

97116
```java
98-
117+
class Solution {
118+
public int[] timeTaken(int[] arrival, int[] state) {
119+
Deque<Integer>[] q = new Deque[2];
120+
Arrays.setAll(q, i -> new ArrayDeque<>());
121+
int n = arrival.length;
122+
int t = 0, i = 0, st = 1;
123+
int[] ans = new int[n];
124+
while (i < n || !q[0].isEmpty() || !q[1].isEmpty()) {
125+
while (i < n && arrival[i] <= t) {
126+
q[state[i]].add(i++);
127+
}
128+
if (!q[0].isEmpty() && !q[1].isEmpty()) {
129+
ans[q[st].poll()] = t;
130+
} else if (!q[0].isEmpty() || !q[1].isEmpty()) {
131+
st = q[0].isEmpty() ? 1 : 0;
132+
ans[q[st].poll()] = t;
133+
} else {
134+
st = 1;
135+
}
136+
++t;
137+
}
138+
return ans;
139+
}
140+
}
99141
```
100142

101143
#### C++
102144

103145
```cpp
104-
146+
class Solution {
147+
public:
148+
vector<int> timeTaken(vector<int>& arrival, vector<int>& state) {
149+
int n = arrival.size();
150+
queue<int> q[2];
151+
int t = 0, i = 0, st = 1;
152+
vector<int> ans(n);
153+
154+
while (i < n || !q[0].empty() || !q[1].empty()) {
155+
while (i < n && arrival[i] <= t) {
156+
q[state[i]].push(i++);
157+
}
158+
159+
if (!q[0].empty() && !q[1].empty()) {
160+
ans[q[st].front()] = t;
161+
q[st].pop();
162+
} else if (!q[0].empty() || !q[1].empty()) {
163+
st = q[0].empty() ? 1 : 0;
164+
ans[q[st].front()] = t;
165+
q[st].pop();
166+
} else {
167+
st = 1;
168+
}
169+
170+
++t;
171+
}
172+
173+
return ans;
174+
}
175+
};
105176
```
106177

107178
#### Go
108179

109180
```go
181+
func timeTaken(arrival []int, state []int) []int {
182+
n := len(arrival)
183+
q := [2][]int{}
184+
t, i, st := 0, 0, 1
185+
ans := make([]int, n)
186+
187+
for i < n || len(q[0]) > 0 || len(q[1]) > 0 {
188+
for i < n && arrival[i] <= t {
189+
q[state[i]] = append(q[state[i]], i)
190+
i++
191+
}
192+
193+
if len(q[0]) > 0 && len(q[1]) > 0 {
194+
ans[q[st][0]] = t
195+
q[st] = q[st][1:]
196+
} else if len(q[0]) > 0 || len(q[1]) > 0 {
197+
if len(q[0]) == 0 {
198+
st = 1
199+
} else {
200+
st = 0
201+
}
202+
ans[q[st][0]] = t
203+
q[st] = q[st][1:]
204+
} else {
205+
st = 1
206+
}
207+
208+
t++
209+
}
210+
211+
return ans
212+
}
213+
```
110214

215+
#### TypeScript
216+
217+
```ts
218+
function timeTaken(arrival: number[], state: number[]): number[] {
219+
const n = arrival.length;
220+
const q: number[][] = [[], []];
221+
let [t, i, st] = [0, 0, 1];
222+
const ans: number[] = Array(n).fill(0);
223+
224+
while (i < n || q[0].length || q[1].length) {
225+
while (i < n && arrival[i] <= t) {
226+
q[state[i]].push(i++);
227+
}
228+
229+
if (q[0].length && q[1].length) {
230+
ans[q[st][0]] = t;
231+
q[st].shift();
232+
} else if (q[0].length || q[1].length) {
233+
st = q[0].length ? 0 : 1;
234+
ans[q[st][0]] = t;
235+
q[st].shift();
236+
} else {
237+
st = 1;
238+
}
239+
240+
t++;
241+
}
242+
243+
return ans;
244+
}
111245
```
112246

113247
<!-- tabs:end -->
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public:
3+
vector<int> timeTaken(vector<int>& arrival, vector<int>& state) {
4+
int n = arrival.size();
5+
queue<int> q[2];
6+
int t = 0, i = 0, st = 1;
7+
vector<int> ans(n);
8+
9+
while (i < n || !q[0].empty() || !q[1].empty()) {
10+
while (i < n && arrival[i] <= t) {
11+
q[state[i]].push(i++);
12+
}
13+
14+
if (!q[0].empty() && !q[1].empty()) {
15+
ans[q[st].front()] = t;
16+
q[st].pop();
17+
} else if (!q[0].empty() || !q[1].empty()) {
18+
st = q[0].empty() ? 1 : 0;
19+
ans[q[st].front()] = t;
20+
q[st].pop();
21+
} else {
22+
st = 1;
23+
}
24+
25+
++t;
26+
}
27+
28+
return ans;
29+
}
30+
};

0 commit comments

Comments
 (0)