File tree 5 files changed +164
-6
lines changed
1200-1299/1279.Traffic Light Controlled Intersection
2400-2499/2447.Number of Subarrays With GCD Equal to K
5 files changed +164
-6
lines changed Original file line number Diff line number Diff line change 87
87
88
88
<!-- tabs:start -->
89
89
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
+ ```
91
145
92
- ``` sql
146
+ ### ** ...**
147
+
148
+ ```
93
149
94
150
```
95
151
Original file line number Diff line number Diff line change 81
81
82
82
<!-- tabs:start -->
83
83
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
+ ```
85
139
86
- ``` sql
140
+ ### ** ...**
141
+
142
+ ```
87
143
88
144
```
89
145
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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 ()
Original file line number Diff line number Diff line change 47
47
48
48
** 方法一:直接枚举**
49
49
50
- 对于每个子数组,我们可以枚举其左右端点,计算出其最大公因数,然后判断是否等于 ` k ` 即可。
50
+ 对于每个子数组,我们可以枚举其左右端点,计算出其最大公因数,然后判断是否等于 $k$ 即可。
51
51
52
- 时间复杂度 $O(n^2\times log M)。 其中 $n$ 为数组 ` nums ` 的长度, $M$ 为数组 ` nums ` 中的最大值。
52
+ 时间复杂度 $O(n^2\times log M)$, 其中 $n$ 和 $M$ 分别是数组 ` nums ` 的长度和数组 ` nums ` 中的最大值。
53
53
54
54
<!-- tabs:start -->
55
55
You can’t perform that action at this time.
0 commit comments