Leetcode Problem 2534. Time Taken to Cross the Door

2534. Time Taken to Cross the Door

Leetcode Solutions

Simulation with Two Queues

  1. Initialize two empty queues: enterQueue and exitQueue.
  2. Initialize an array result to store the time each person crosses the door.
  3. Initialize currentTime to 0 and lastAction to -1 (indicating the door was not used in the previous second).
  4. Iterate over the arrival and state arrays: a. For each person, if their state is 0 (enter), enqueue their index to enterQueue. b. If their state is 1 (exit), enqueue their index to exitQueue.
  5. While either enterQueue or exitQueue is not empty: a. If currentTime is less than the arrival time of the person at the front of both queues, increment currentTime and set lastAction to -1. b. Otherwise, determine which queue has priority based on lastAction and the rules. c. Dequeue the person from the queue with priority and set their crossing time in result. d. Update lastAction based on the action taken and increment currentTime.
  6. Return the result array.
UML Thumbnail

Simulation with Priority Handling

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...