Time Taken to Cross the Door - Problem

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 these 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 array answer of size n where answer[i] is the second at which the ith person crosses the door.

Note: 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 & Output

Example 1 — Basic Priority Rules
$ Input: arrival = [0,1,1,2], state = [0,1,0,1]
Output: [0,2,1,3]
💡 Note: Person 0 (enter) arrives at time 0, crosses at time 0. At time 1, persons 1 (exit) and 2 (enter) arrive. Since door unused last second, person 1 (exit) has priority and crosses at time 2. Person 2 (enter) crosses at time 1. Person 3 (exit) arrives at time 2 and crosses at time 3.
Example 2 — Same Direction Priority
$ Input: arrival = [0,0,0], state = [1,1,1]
Output: [0,1,2]
💡 Note: All three people want to exit and arrive at time 0. Door is unused, so exit has priority. Person 0 goes first (lowest index), then 1, then 2.
Example 3 — Enter After Enter
$ Input: arrival = [0,1,2], state = [0,0,1]
Output: [0,1,2]
💡 Note: Person 0 enters at time 0. Person 1 arrives at time 1 and wants to enter - since last action was enter, person 1 enters at time 1. Person 2 arrives at time 2 and exits at time 2.

Constraints

  • 1 ≤ n ≤ 105
  • 0 ≤ arrival[i] ≤ 105
  • arrival is sorted in non-decreasing order
  • state[i] is either 0 or 1

Visualization

Tap to expand
Time Taken to Cross the Door INPUT DOOR Persons (0-3) P0 Enter P1 Exit P2 Enter P3 Exit arrival[] 0 1 1 2 state[] 0 1 0 1 Enter(0) Exit(1) ALGORITHM STEPS 1 Create Two Queues enterQ and exitQ for persons by direction 2 Process Each Second Add arrived persons to their queues 3 Apply Priority Rules Idle: Exit first Otherwise: same dir 4 Record Cross Time Store current time in answer[person_id] Simulation Trace t Person Action 0 P0 Enter (idle) 1 P2 Enter (prev=enter) 2 P1 Exit (waiting) 3 P3 Exit (prev=exit) FINAL RESULT answer[] = [0, 2, 1, 3] Door Usage Timeline t=0 t=1 t=2 t=3 P0 P2 P1 P3 Crossing Times Person 0 crosses at t=0 Person 1 crosses at t=2 Person 2 crosses at t=1 Person 3 crosses at t=3 Output: [0, 2, 1, 3] Key Insight: Use two queues (enter/exit) to manage waiting persons. At each time step, check the previous door state: if idle or last was exit, prioritize exit queue; if last was enter, prioritize enter queue. Process smallest index first when multiple persons want same direction. Time: O(n), Space: O(n) TutorialsPoint - Time Taken to Cross the Door | Queue-Based Simulation Approach
Asked in
Google 45 Amazon 38 Microsoft 22
28.5K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen