Skip to content

Commit fdf4d2e

Browse files
authored
Create traffic-light-controlled-intersection.py
1 parent 5dea38a commit fdf4d2e

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
import threading
5+
6+
7+
class TrafficLight(object):
8+
9+
def __init__(self):
10+
self.__lock = threading.Lock()
11+
self.__light = 1
12+
13+
def carArrived(self, carId, roadId, direction, turnGreen, crossCar):
14+
"""
15+
:type roadId: int --> // ID of the car
16+
:type carId: int --> // ID of the road the car travels on. Can be 1 (road A) or 2 (road B)
17+
:type direction: int --> // Direction of the car
18+
:type turnGreen: method --> // Use turnGreen() to turn light to green on current road
19+
:type crossCar: method --> // Use crossCar() to make car cross the intersection
20+
:rtype: void
21+
"""
22+
with self.__lock:
23+
if self.__light != roadId:
24+
self.__light = roadId
25+
turnGreen()
26+
crossCar()

0 commit comments

Comments
 (0)