Practice/Google/Leetcode 1279. Traffic Light Controlled Intersection
Leetcode 1279. Traffic Light Controlled Intersection
CodingMust
Problem
You need to simulate a four-way intersection controlled by a traffic light. The intersection allows traffic in two perpendicular directions:
- North-South (directions 0 and 2): Cars traveling North or South
- East-West (directions 1 and 3): Cars traveling East or West
The traffic light alternates between these two states. Initially, the light allows North-South traffic. The light switches to allow the perpendicular direction only when:
- There are no more cars in the current direction's queue, AND
- There is at least one car waiting in the perpendicular direction
Cars arrive at the intersection at specific times. Each car has an arrival time and requires a certain amount of time to cross the intersection. Cars from the same direction cross in the order they arrive (FIFO). A car can only start crossing when:
- The light allows its direction
- No other car from its direction group is currently crossing
- The car has arrived at the intersection
Your task is to determine when the last car finishes crossing the intersection.
Requirements
- Implement a traffic light controller that switches between North-South and East-West traffic
- Process cars in FIFO order within their direction group (North-South or East-West)
- The light only switches when the current direction has no cars waiting or crossing AND the perpendicular direction has cars waiting
- Return the time at which the final car completes crossing
Constraints
- 1 ≤ arrivals.length ≤ 100
- Each arrival is [car_id, direction, arrival_time, travel_time]
- 0 ≤ direction ≤ 3 (0=North, 1=East, 2=South, 3=West)
- 0 ≤ arrival_time ≤ 10,000
- 1 ≤ travel_time ≤ 100
- All car_id values are unique
- arrivals may not be sorted by arrival time
Examples
Example 1:
`
Input: arrivals = [[1, 0, 0, 2], [2, 0, 1, 1]]
Output: 3
Explanation:
- Car 1 arrives at time 0, going North, needs 2 seconds
- Car 2 arrives at time 1, going North, needs 1 second
- Light starts allowing North-South traffic
- Car 1 crosses from time 0 to 2
- Car 2 crosses from time 2 to 3
- Last car finishes at time 3
`
Example 2:
`
Input: arrivals = [[1, 0, 0, 5], [2, 1, 2, 3]]
Output: 8
Explanation:
- Car 1 (North) arrives at 0, needs 5 seconds
- Car 2 (East) arrives at 2, needs 3 seconds
- Light starts allowing North-South
- Car 1 crosses from time 0 to 5
- At time 5, no North-South cars remain, light switches to East-West
- Car 2 crosses from time 5 to 8
- Last car finishes at time 8
`
Example 3:
`
Input: arrivals = [[1, 0, 5, 2], [2, 1, 3, 4]]
Output: 9
Explanation:
- Car 2 (East) arrives at 3, needs 4 seconds
- Car 1 (North) arrives at 5, needs 2 seconds
- Light starts allowing North-South, but no cars yet
- Car 2 arrives at 3 for East-West direction, but must wait
- Car 1 arrives at 5, can start immediately (North-South allowed)
- Car 1 crosses from 5 to 7
- Light switches to East-West at time 7
- Car 2 has been waiting since time 3, crosses from 7 to 11... wait no
- Actually car 2 waits until light switches, crosses 7 to 11... no that's 4 seconds
- Car 2 crosses from time 7 to 7+4=11... but answer says 9
- Let me reconsider: if light starts NS, and first car is EW at time 3, light CAN switch immediately since no NS cars are present. Car 2 crosses 3-7. Car 1 arrives at 5 (during car 2's crossing), must wait. At time 7, light switches to NS. Car 1 crosses 7-9.
`