Skip to content

Commit 2b52653

Browse files
committed
feat: add solutions to lc problem: No.1279
No.1279.Traffic Light Controlled Intersection
1 parent 837a82f commit 2b52653

File tree

5 files changed

+164
-6
lines changed

5 files changed

+164
-6
lines changed

solution/1200-1299/1279.Traffic Light Controlled Intersection/README.md

+58-2
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,65 @@
8787

8888
<!-- tabs:start -->
8989

90-
### **SQL**
90+
### **Python3**
91+
92+
```python
93+
from threading import Lock
94+
95+
96+
class TrafficLight:
97+
def __init__(self):
98+
self.lock = Lock()
99+
self.road = 1
100+
101+
def carArrived(
102+
self,
103+
carId: int, # ID of the car
104+
# ID of the road the car travels on. Can be 1 (road A) or 2 (road B)
105+
roadId: int,
106+
direction: int, # Direction of the car
107+
# Use turnGreen() to turn light to green on current road
108+
turnGreen: 'Callable[[], None]',
109+
# Use crossCar() to make car cross the intersection
110+
crossCar: 'Callable[[], None]'
111+
) -> None:
112+
self.lock.acquire()
113+
if self.road != roadId:
114+
self.road = roadId
115+
turnGreen()
116+
crossCar()
117+
self.lock.release()
118+
```
119+
120+
### **Java**
121+
122+
```java
123+
class TrafficLight {
124+
private int road = 1;
125+
126+
public TrafficLight() {
127+
128+
}
129+
130+
public synchronized void carArrived(
131+
int carId, // ID of the car
132+
int roadId, // ID of the road the car travels on. Can be 1 (road A) or 2 (road B)
133+
int direction, // Direction of the car
134+
Runnable turnGreen, // Use turnGreen.run() to turn light to green on current road
135+
Runnable crossCar // Use crossCar.run() to make car cross the intersection
136+
) {
137+
if (roadId != road) {
138+
turnGreen.run();
139+
road = roadId;
140+
}
141+
crossCar.run();
142+
}
143+
}
144+
```
91145

92-
```sql
146+
### **...**
147+
148+
```
93149
94150
```
95151

solution/1200-1299/1279.Traffic Light Controlled Intersection/README_EN.md

+58-2
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,65 @@
8181

8282
<!-- tabs:start -->
8383

84-
### **SQL**
84+
### **Python3**
85+
86+
```python
87+
from threading import Lock
88+
89+
90+
class TrafficLight:
91+
def __init__(self):
92+
self.lock = Lock()
93+
self.road = 1
94+
95+
def carArrived(
96+
self,
97+
carId: int, # ID of the car
98+
# ID of the road the car travels on. Can be 1 (road A) or 2 (road B)
99+
roadId: int,
100+
direction: int, # Direction of the car
101+
# Use turnGreen() to turn light to green on current road
102+
turnGreen: 'Callable[[], None]',
103+
# Use crossCar() to make car cross the intersection
104+
crossCar: 'Callable[[], None]'
105+
) -> None:
106+
self.lock.acquire()
107+
if self.road != roadId:
108+
self.road = roadId
109+
turnGreen()
110+
crossCar()
111+
self.lock.release()
112+
```
113+
114+
### **Java**
115+
116+
```java
117+
class TrafficLight {
118+
private int road = 1;
119+
120+
public TrafficLight() {
121+
122+
}
123+
124+
public synchronized void carArrived(
125+
int carId, // ID of the car
126+
int roadId, // ID of the road the car travels on. Can be 1 (road A) or 2 (road B)
127+
int direction, // Direction of the car
128+
Runnable turnGreen, // Use turnGreen.run() to turn light to green on current road
129+
Runnable crossCar // Use crossCar.run() to make car cross the intersection
130+
) {
131+
if (roadId != road) {
132+
turnGreen.run();
133+
road = roadId;
134+
}
135+
crossCar.run();
136+
}
137+
}
138+
```
85139

86-
```sql
140+
### **...**
141+
142+
```
87143
88144
```
89145

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class TrafficLight {
2+
private int road = 1;
3+
4+
public TrafficLight() {
5+
6+
}
7+
8+
public synchronized void carArrived(
9+
int carId, // ID of the car
10+
int roadId, // ID of the road the car travels on. Can be 1 (road A) or 2 (road B)
11+
int direction, // Direction of the car
12+
Runnable turnGreen, // Use turnGreen.run() to turn light to green on current road
13+
Runnable crossCar // Use crossCar.run() to make car cross the intersection
14+
) {
15+
if (roadId != road) {
16+
turnGreen.run();
17+
road = roadId;
18+
}
19+
crossCar.run();
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from threading import Lock
2+
3+
4+
class TrafficLight:
5+
def __init__(self):
6+
self.lock = Lock()
7+
self.road = 1
8+
9+
def carArrived(
10+
self,
11+
carId: int, # ID of the car
12+
# ID of the road the car travels on. Can be 1 (road A) or 2 (road B)
13+
roadId: int,
14+
direction: int, # Direction of the car
15+
# Use turnGreen() to turn light to green on current road
16+
turnGreen: 'Callable[[], None]',
17+
# Use crossCar() to make car cross the intersection
18+
crossCar: 'Callable[[], None]'
19+
) -> None:
20+
self.lock.acquire()
21+
if self.road != roadId:
22+
self.road = roadId
23+
turnGreen()
24+
crossCar()
25+
self.lock.release()

solution/2400-2499/2447.Number of Subarrays With GCD Equal to K/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@
4747

4848
**方法一:直接枚举**
4949

50-
对于每个子数组,我们可以枚举其左右端点,计算出其最大公因数,然后判断是否等于 `k` 即可。
50+
对于每个子数组,我们可以枚举其左右端点,计算出其最大公因数,然后判断是否等于 $k$ 即可。
5151

52-
时间复杂度 $O(n^2\times log M)其中 $n$ 为数组 `nums` 的长度,$M$ 为数组 `nums` 中的最大值。
52+
时间复杂度 $O(n^2\times log M)$,其中 $n$ $M$ 分别是数组 `nums` 的长度和数组 `nums` 中的最大值。
5353

5454
<!-- tabs:start -->
5555

0 commit comments

Comments
 (0)