2534. Time Taken to Cross the Door
Practice Time Taken to Cross the Door for free on NudgeCode: Hard, Array & Hash Table pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.
n people, labelled 0 .. n-1, use one door. Person i arrives at second arrival[i] (non-decreasing) and wants to enter (state[i] = 0) or exit (state[i] = 1). Each second at most one person crosses the door, taking exactly that second. When several people are waiting:
- If the door was not used in the previous second, people who want to exit go first.
- If the door was used to enter in the previous second, people who want to enter go first.
- If it was used to exit in the previous second, people who want to exit go first.
- Among people going the same way, the smallest label goes first.
Return answer, where answer[i] is the second at which person i crosses the door.
Example
arrival = [0,1,1,2,4], state = [0,1,0,0,1] -> [0,3,1,2,4]
At second 0 only person 0 waits and enters. At 1, persons 1 (exit) and 2 (enter) wait; the door was just used to enter, so person 2 enters. At 2, person 3 enters too. At 3 person 1 exits; at 4 person 4 exits.
Constraints
- 1 <= n <= 10^5
- 0 <= arrival[i] <= n, arrival is non-decreasing
- state[i] is 0 or 1