Time Taken to Cross the Door - Problem

Imagine a busy office building with a single revolving door that everyone must use to enter or exit. You're tasked with managing the flow of n people (numbered 0 to n-1) who arrive at different times.

Each person takes exactly 1 second to cross the door, and you're given:

  • arrival[i]: The time when person i arrives at the door
  • state[i]: Whether person i wants to enter (0) or exit (1)

When multiple people want to use the door simultaneously, they follow strict priority rules:

  1. If the door was unused in the previous second → Exiters go first
  2. If the door was used for enteringEnterers continue first
  3. If the door was used for exitingExiters continue first
  4. Among people going the same direction → Lowest index wins

Your goal: Return an array where answer[i] is the exact second when person i crosses the door.

Input & Output

example_1.py — Basic Door Usage
$ Input: arrival = [0,1,1,2,4], state = [0,1,0,0,1]
Output: [0,2,1,4,3]
💡 Note: Person 0 arrives at time 0 and wants to enter (crosses at time 0). At time 1, both person 1 (exit) and person 2 (enter) arrive. Since door was just used for entering, person 2 (enter) has priority and crosses at time 1. Person 1 crosses at time 2. Person 3 arrives at time 2 and crosses at time 4. Person 4 arrives at time 4 but must wait since person 3 is using door, so crosses at time 3.
example_2.py — Exit Priority When Idle
$ Input: arrival = [0,0,0], state = [1,1,0]
Output: [0,1,2]
💡 Note: All three people arrive at time 0. Since the door hasn't been used, exiters have priority. Person 0 and 1 both want to exit, but person 0 has lower index so goes first at time 0. Person 1 exits at time 1. Person 2 enters at time 2.
example_3.py — Continuation Priority
$ Input: arrival = [0,1,2], state = [1,1,1]
Output: [0,1,2]
💡 Note: Person 0 exits at time 0. Person 1 arrives at time 1. Since the door was previously used for exiting, person 1 (also exiting) has priority and crosses at time 1. Person 2 arrives at time 2 and crosses immediately since no one else is waiting.

Visualization

Tap to expand
Revolving DoorEnter QueuePeople wanting to enterExit QueuePeople wanting to exit02314StatePriority: Idle → Exit first | Enter usage → Enter continues | Exit usage → Exit continues
Understanding the Visualization
1
People Arrive
People arrive at scheduled times and join appropriate queues
2
Check Door State
Look at how the door was used in the previous second
3
Apply Priority Rules
If idle → exiters first. If used for entering → enterers continue. If used for exiting → exiters continue
4
Process Person
The person with lowest index from the priority group crosses the door
5
Update State
Record the crossing time and update door usage state for next iteration
Key Takeaway
🎯 Key Insight: Use separate queues and track door state to efficiently simulate the crossing process without checking every second individually.

Time & Space Complexity

Time Complexity
⏱️
O(n log n)

Sorting people by arrival time dominates, then O(n) to process each person once

n
2n
Linearithmic
Space Complexity
O(n)

Space for queues, result array, and sorting

n
2n
Linearithmic Space

Constraints

  • 1 ≤ n ≤ 105
  • arrival.length == state.length == n
  • 0 ≤ arrival[i] ≤ 105
  • arrival is sorted in non-decreasing order
  • state[i] is either 0 or 1
Asked in
Amazon 15 Microsoft 12 Google 8 Meta 6
23.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