There are n persons numbered from 0 to n - 1 and a door. Each person can enter or exit through the door once, taking one second.
You are given a non-decreasing integer array arrival of size n, where
arrival[i] is the arrival time of the ith person at the door. You are also given an array state of size n, where state[i] is 0 if person i
wants to enter through the door or 1 if they want to exit through the door.
If two or more persons want to use the door at the same time, they follow the following rules:
If the door was not used in the previous second, then the person who wants to exit goes first.
If the door was used in the previous second for entering , the person who wants to enter goes first.
If the door was used in the previous second for exiting , the person who wants to exit goes first.
If multiple persons want to go in the same direction, the person with the smallest index goes first.
Return an arrayanswerof sizenwhereanswer[i]is the second at which theithperson crosses the door.
Note that:
Only one person can cross the door at each second.
A person may arrive at the door and wait without entering or exiting to follow the mentioned rules.
Input: arrival =[0,1,1,2,4], state =[0,1,0,0,1]Output: [0,3,1,2,4]Explanation: At each second we have the following:- At t =0: Person 0is the only one who wants to enter, so they just enter through the door.- At t =1: Person 1 wants to exit, and person 2 wants to enter. Since the door was used the previous second for entering, person 2 enters.- At t =2: Person 1 still wants to exit, and person 3 wants to enter. Since the door was used the previous second for entering, person 3 enters.- At t =3: Person 1is the only one who wants to exit, so they just exit through the door.- At t =4: Person 4is the only one who wants to exit, so they just exit through the door.
Example 2:
1
2
3
4
5
6
Input: arrival =[0,0,0], state =[1,0,1]Output: [0,2,1]Explanation: At each second we have the following:- At t =0: Person 1 wants to enter while persons 0 and 2 want to exit. Since the door was not used in the previous second, the persons who want to exit get to go first. Since person 0 has a smaller index, they exit first.- At t =1: Person 1 wants to enter, and person 2 wants to exit. Since the door was used in the previous second for exiting, person 2 exits.- At t =2: Person 1is the only one who wants to enter, so they just enter through the door.
Use two queues to track people waiting to enter and exit. At each second, process the correct person according to the previous door usage and arrival times.
from collections import deque
from typing import List
classSolution:
deftimeTaken(self, arrival: List[int], state: List[int]) -> List[int]:
n = len(arrival)
ans = [0] * n
enter = deque()
exit = deque()
i =0 t =0 last =-1# -1: unused, 0: enter, 1: exitwhile i < n or enter or exit:
while i < n and arrival[i] <= t:
if state[i] ==0:
enter.append(i)
else:
exit.append(i)
i +=1if exit and (last ==1or last ==-1ornot enter):
idx = exit.popleft()
ans[idx] = t
last =1elif enter:
idx = enter.popleft()
ans[idx] = t
last =0else:
last =-1 t = arrival[i] if i < n else t +1continue t +=1return ans
import java.util.*;
classSolution {
publicint[]timeTaken(int[] arrival, int[] state) {
int n = arrival.length;
int[] ans =newint[n];
Queue<Integer> enter =new ArrayDeque<>();
Queue<Integer> exit =new ArrayDeque<>();
int i = 0, t = 0, last =-1;
while (i < n ||!enter.isEmpty() ||!exit.isEmpty()) {
while (i < n && arrival[i]<= t) {
if (state[i]== 0) enter.offer(i);
else exit.offer(i);
i++;
}
if (!exit.isEmpty() && (last == 1 || last ==-1 || enter.isEmpty())) {
int idx = exit.poll();
ans[idx]= t;
last = 1;
} elseif (!enter.isEmpty()) {
int idx = enter.poll();
ans[idx]= t;
last = 0;
} else {
last =-1;
t = (i < n) ? arrival[i] : t + 1;
continue;
}
t++;
}
return ans;
}
}