当前位置:网站首页>LeetCode-1279. Traffic light intersection
LeetCode-1279. Traffic light intersection
2022-07-06 19:09:00 【Border wanderer】
This is the intersection of two roads . The first way is A road , The vehicle can move along 1 Heading from north to south , Can also be along 2 Heading from south to north . The second way is B road , The vehicle can move along 3 Drive from west to East in direction No , Can also be along 4 Drive from east to West in direction No .

Every road has a traffic light in front of the intersection . Traffic lights can be red or green .
The green light means that vehicles in both directions can pass the intersection .
The red light means that vehicles in both directions are not allowed to pass through the intersection , You must wait for the green light to come on .
Traffic lights on both roads cannot be green at the same time . It means , When A When the green light on the road comes on ,B The red light on the road will light up ; When B When the green light on the road comes on ,A The red light on the road will light up .
At the beginning of the ,A The green light on the road ,B The red light on the road is on . When the green light on a road lights up , All vehicles can pass through the intersection from any two directions , Until the green light on the other road . Vehicles on different roads cannot pass through the intersection at the same time .
Design a traffic light control system without deadlock for this intersection .
Implementation function void carArrived(carId, roadId, direction, turnGreen, crossCar) :
carId Is the number of the arriving vehicle .
roadId Is the number of the road where the vehicle is located .
direction For the direction of the vehicle .
turnGreen It's a function , Calling this function will cause the green light on the current road to light up .
crossCar It's a function , Calling this function will allow the vehicle to pass through the intersection .
When your answer avoids the deadlock of vehicles at the intersection , This answer will be considered correct . Turn on the green light when the green light has been on at the intersection , This answer will be considered wrong .
Example 1:
Input : cars = [1,3,5,2,4], directions = [2,1,2,4,3], arrivalTimes = [10,20,30,40,50]
Output : [
"Car 1 Has Passed Road A In Direction 2", // A The traffic lights on the road are green ,1 Car No. 1 can pass through the intersection .
"Car 3 Has Passed Road A In Direction 1", // The traffic lights are still green ,3 Car No. 2 goes through the intersection .
"Car 5 Has Passed Road A In Direction 2", // The traffic lights are still green ,5 Car No. 2 goes through the intersection .
"Traffic Light On Road B Is Green", // 2 Car number is B Road request green light .
"Car 2 Has Passed Road B In Direction 4", // B The green light on the road is now on ,2 Car No. 2 goes through the intersection .
"Car 4 Has Passed Road B In Direction 3" // The traffic lights are still green ,4 Car No. 2 goes through the intersection .
]
Example 2:
Input : cars = [1,2,3,4,5], directions = [2,4,3,3,1], arrivalTimes = [10,20,30,40,40]
Output : [
"Car 1 Has Passed Road A In Direction 2", // A The traffic lights on the road are green ,1 Car No. 1 can pass through the intersection .
"Traffic Light On Road B Is Green", // 2 Car number is B Road request green light .
"Car 2 Has Passed Road B In Direction 4", // B The green light on the road is now on ,2 Car No. 2 goes through the intersection .
"Car 3 Has Passed Road B In Direction 3", // B The green light on the road is now on ,3 Car No. 2 goes through the intersection .
"Traffic Light On Road A Is Green", // 5 Car number is A Road request green light .
"Car 5 Has Passed Road A In Direction 1", // A The green light on the road is now on ,5 Car No. 2 goes through the intersection .
"Traffic Light On Road B Is Green", // 4 Car number is B Road request green light .4 Car No. 1 is waiting for the light at the intersection , until 5 Car No. 2 goes through the intersection ,B The green light on the road is on .
"Car 4 Has Passed Road B In Direction 3" // B The green light on the road is now on ,4 Car No. 2 goes through the intersection .
]
explain : This is a deadlock free solution . Be careful , stay A The green light on the road 、5 Give way to... Before car No 4 Car No. 2 passed , It is also a correct and acceptable scheme .
Tips :
1 <= cars.length <= 20
cars.length = directions.length
cars.length = arrivalTimes.length
cars All values in are unique .
1 <= directions[i] <= 4
arrivalTimes Non decreasing .
#include<iostream>
#include<functional>
#include<mutex>
using namespace std;
class TrafficLight {
public:
TrafficLight() {
road = 'A';
}
void carArrived(
int carId, // ID of the car
int roadId, // ID of the road the car travels on. Can be 1 (road A) or 2 (road B)
int direction, // Direction of the car
function<void()> turnGreen, // Use turnGreen() to turn light to green on current road
function<void()> crossCar // Use crossCar() to make car cross the intersection
) {
std::lock_guard<mutex> lck(mtx);
if (direction <= 2 && road != 'A') {
turnGreen();
road = 'A';
}
else if(direction > 2 && road!='B') {
turnGreen();
road = 'B';
}
crossCar();
}
private:
char road = 'A';
std::mutex mtx;
};边栏推荐
- C#/VB.NET 给PDF文档添加文本/图像水印
- 史上超级详细,想找工作的你还不看这份资料就晚了
- R语言ggplot2可视化:使用ggpubr包的ggstripchart函数可视化分组点状条带图(dot strip plot)、设置add参数为不同水平点状条带图添加箱图
- 中缀表达式转后缀表达式详细思路及代码实现
- 美庐生物IPO被终止:年营收3.85亿 陈林为实控人
- Tongyu Xincai rushes to Shenzhen Stock Exchange: the annual revenue is 947million Zhang Chi and Su Shiguo are the actual controllers
- Describe the process of key exchange
- Digital "new" operation and maintenance of energy industry
- LeetCode-1279. 红绿灯路口
- About static type, dynamic type, ID, instancetype
猜你喜欢

多线程基础:线程基本概念与线程的创建

全套教学资料,阿里快手拼多多等7家大厂Android面试真题
![A wearable arm device for night and sleeveless blood pressure measurement [translation]](/img/fd/947a38742ab1c4009ec6aa7405a573.png)
A wearable arm device for night and sleeveless blood pressure measurement [translation]

ACTF 2022圆满落幕,0ops战队二连冠!!

MRO industrial products enterprise procurement system: how to refine procurement collaborative management? Industrial products enterprises that want to upgrade must see!

Method of accessing mobile phone storage location permission under non root condition

PMP每日一练 | 考试不迷路-7.6

数学知识——高斯消元(初等行变换解方程组)代码实现

提前解锁 2 大直播主题!今天手把手教你如何完成软件包集成?|第 29-30 期

Based on butterfly species recognition
随机推荐
Xingnuochi technology's IPO was terminated: it was planned to raise 350million yuan, with an annual revenue of 367million yuan
包装行业商业供应链管理平台解决方案:布局智慧供应体系,数字化整合包装行业供应链
青龙面板最近的库
一种用于夜间和无袖测量血压手臂可穿戴设备【翻译】
提前解锁 2 大直播主题!今天手把手教你如何完成软件包集成?|第 29-30 期
裕太微冲刺科创板:拟募资13亿 华为与小米基金是股东
AutoCAD - what is the default lineweight for centerline drawing and CAD? Can I modify it?
Help improve the professional quality of safety talents | the first stage of personal ability certification and assessment has been successfully completed!
ROS自定义消息发布订阅示例
AUTOCAD——中心线绘制、CAD默认线宽是多少?可以修改吗?
Simple understanding of MySQL database
ORACLE进阶(四)表连接讲解
ROS custom message publishing subscription example
The nearest library of Qinglong panel
全套教学资料,阿里快手拼多多等7家大厂Android面试真题
Lucun smart sprint technology innovation board: annual revenue of 400million, proposed to raise 700million
How to type multiple spaces when editing CSDN articles
Graffiti intelligence is listed on the dual main board in Hong Kong: market value of 11.2 billion Hong Kong, with an annual revenue of 300 million US dollars
基于ppg和fft神经网络的光学血压估计【翻译】
AIRIOT物联网平台赋能集装箱行业构建【焊接工位信息监控系统】