Determine Pass Times for Single-Lane Security Gate
Stacks & QueuesAuto-graded · 22 test casesLast updated
Simulate the scheduling of access requests through a shared resource using state tracking and queues under conflict-resolution rules.
A single-lane security gate processes individuals moving in two possible directions: entering (0) or leaving (1). Exactly one person can traverse the gate per second.
The gate applies the following arbitration logic to resolve conflicts when multiple people are queued:
- If the gate was operated during the previous second (
t - 1), it gives priority to people traveling in the same direction as that previous operation. - If the gate was idle during the previous second (
t - 1), or if people are waiting in only one direction, preference is given to the leaving direction (1). - If multiple people waiting in the same direction arrived at different times, the person who arrived earlier goes first. If they arrived at the exact same time, the person with the smaller initial index goes first.
You are given two arrays of equal length, times and direction:
times[i]specifies the timestamp (in seconds) when personireaches the gate area.direction[i]indicates whether personiwants to enter (0) or leave (1).
Return an array results of length $N$, where results[i] is the timestamp at which person i completes their passage through the gate.
Constraints
- $1 \le N \le 10^5$, where $N$ is the number of people.
timesis sorted in non-decreasing order (times[i] <= times[i+1]).- $0 \le \text{times}[i] \le 10^9$.
direction[i]is either0or1.len(times) == len(direction).
Examples
Example 1
Input: get_turnstile_times([0, 0, 1, 5], [0, 1, 1, 0])
Output: [2, 0, 1, 5]
Example 2
Input: get_turnstile_times([0, 1, 1, 3, 3], [0, 1, 0, 0, 1])
Output: [0, 2, 1, 4, 3]
Hints
Hint 1
Use two separate double-ended queues (deques) to track waiting individuals for each direction (0 for entering, 1 for leaving).
Hint 2
Instead of incrementing time second-by-second up to 10^9, jump directly to the next person's arrival time whenever both waiting queues are empty.
Hint 3
Maintain two state variables: the direction used during the previous passage and a boolean flag indicating whether the gate was used in the immediately preceding second.
Worked solution
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Method Explanation
The problem can be modeled as a discrete event simulation. We track:
- Two FIFO queues (
waiting[0]for entering,waiting[1]for leaving) storing the indices of waiting individuals. - The current clock time
current. - The gate's status in the preceding second:
previous_directionandused_previous_second.
In each step of the simulation loop:
- Idle Fast-Forwarding: If both queues are empty and the next person has not arrived yet, we jump
currentdirectly totimes[i]and markused_previous_second = False. This ensures we do not iterate through empty seconds when arrival timestamps are large (e.g., $10^9$). - Arrival Ingestion: We append all individuals whose arrival time is $\le \text{current}$ into their respective direction's queue. Since input arrays are sorted by arrival time, candidates are naturally queued in order of arrival time and initial index.
- Conflict Arbitration:
- If both queues have people waiting, we pick
previous_directionif the gate was used atcurrent - 1, or direction1(leaving) if the gate was idle atcurrent - 1. - If only one queue has people waiting, we serve that queue.
- If both queues have people waiting, we pick
- Execution: We pop the leading candidate from the selected queue, assign
result[person] = current, update the previous state, and incrementcurrentby $1$.
Correctness and Efficiency
- Time Complexity: $\mathcal{O}(N)$. Each person is pushed into a
dequeonce and popped once. Jumping over idle time gaps avoids linear scanning over $10^9$ seconds. - Space Complexity: $\mathcal{O}(N)$ to store the result array and the FIFO queues.
Python Reference Solution
from collections import deque
from typing import List
def get_turnstile_times(times: List[int], direction: List[int]) -> List[int]:
n = len(times)
waiting = [deque(), deque()] # waiting[0]: enter, waiting[1]: exit
result = [0] * n
i = 0
current = 0
previous_direction = 1
used_previous_second = False
while i < n or waiting[0] or waiting[1]:
# Jump over idle gap if no one is waiting
if not waiting[0] and not waiting[1] and i < n and current < times[i]:
current = times[i]
used_previous_second = False
# Enqueue all people arriving at or before the current timestamp
while i < n and times[i] <= current:
waiting[direction[i]].append(i)
i += 1
# Determine which direction gets priority
if waiting[0] and waiting[1]:
chosen = previous_direction if used_previous_second else 1
elif waiting[1]:
chosen = 1
else:
chosen = 0
# Process the chosen person
person = waiting[chosen].popleft()
result[person] = current
previous_direction = chosen
used_previous_second = True
current += 1
return result