Leetcode Problem 1279. Traffic Light Controlled Intersection

1279. Traffic Light Controlled Intersection

Leetcode Solutions

Synchronized Traffic Light Control with Lock

  1. Initialize a lock and a variable to keep track of the current green road.
  2. When a car arrives, acquire the lock to ensure mutual exclusion.
  3. Check if the traffic light for the car's road is green.
    • If not, call turnGreen to change the traffic light to green for the car's road.
    • Update the current green road variable.
  4. Call crossCar to let the car cross the intersection.
  5. Release the lock.
  6. Repeat the process for each car that arrives.
UML Thumbnail

Traffic Light Control with Atomic Boolean

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...