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 Door: Priority-Based SimulationPerson 0T:0, EnterPerson 1T:1, ExitPerson 2T:1, EnterPerson 3T:2, Exit🚪 Door Priority Rules1. Unused → Exit priority2. Last Enter → Enter priority3. Same index → Lower winsCrosses: 0Time 0Crosses: 2Time 1Crosses: 1Time 2Crosses: 3Time 3
Understanding the Visualization
1
Input
People with arrival times and enter/exit states
2
Priority Rules
Apply door usage rules to determine order
3
Output
Array showing when each person crosses
Key Takeaway
🎯 Key Insight: Track door state and use priority rules to determine who crosses next
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