Leetcode Problem 1279. Traffic Light Controlled Intersection
1279. Traffic Light Controlled Intersection
AI Mock Interview
Leetcode Solutions
Synchronized Traffic Light Control with Lock
Solution Idea
Algorithm Steps
Code Implementation
Initialize a lock and a variable to keep track of the current green road.
When a car arrives, acquire the lock to ensure mutual exclusion.
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.
Call
crossCar
to let the car cross the intersection.
Release the lock.
Repeat the process for each car that arrives.
Traffic Light Control with Atomic Boolean
Ask Question
Programming Language
Purpose:
General Question
Debug My Code
image/screenshot of info
(optional)
[+]
Full Screen
Loading...
Get Answer
Suggested Answer
Answer
Full Screen
Copy Answer Code
Loading...