Similar Problems

Similar Problems not available

Traffic Light Controlled Intersection - Leetcode Solution

Companies:

LeetCode:  Traffic Light Controlled Intersection Leetcode Solution

Difficulty: Unknown

Topics: unknown  

The Traffic Light Controlled Intersection problem on LeetCode is a problem that requires the implementation of a system to control the flow of traffic at an intersection. The problem statement describes a scenario where two roads intersect and there are traffic lights placed at each road. The traffic lights alternate between red and green at regular intervals. Vehicles arriving at the intersection must wait if the traffic light they are facing is red, and proceed if it is green.

The problem is to implement a function that takes input parameters describing the current state of the traffic lights and the positions of vehicles waiting at the intersection, and outputs the order in which the vehicles should proceed through the intersection.

The input to the function is as follows:

  • An integer N, representing the number of vehicles waiting at the intersection.
  • A list of tuples, each containing three integers - the first integer is the arrival time of the vehicle, the second integer is the road on which the vehicle is waiting (either road 1 or road 2), and the third integer is the direction in which the vehicle wishes to travel (either north, south, east, or west).

The function should return a list of integers, representing the order in which the vehicles should proceed through the intersection. If two or more vehicles arrive at the same time, the order in which they should proceed is based on the following rules:

  • Vehicles that are already in the intersection should be allowed to complete their movements before any new vehicles are allowed to enter.
  • Vehicles waiting on road 2 should be allowed to proceed before vehicles waiting on road 1.
  • Vehicles should be allowed to proceed in the order that they arrived at the intersection.

To implement the solution, we need to use a data structure to keep track of the state of the intersection. We can use a queue to store the vehicles waiting at each road. We can also use a variable to store the current state of the traffic lights - that is, whether the lights on road 1 are red or green, and whether the lights on road 2 are red or green.

We can start by initializing the queue for each road to be empty. We can also initialize the traffic light state to be such that the lights on road 2 are green and the lights on road 1 are red.

We can then loop through the list of vehicles waiting at the intersection, and for each vehicle, add it to the queue for the appropriate road.

We can then implement a function to process the intersection. Each time this function is called, it should check the state of the traffic lights and allow any vehicles to proceed that are facing a green light. If no vehicles are waiting at the intersection, the function should simply return an empty list. Otherwise, it should return a list of integers representing the order in which the vehicles should proceed through the intersection.

To implement the function, we can first check whether there are any vehicles waiting at the intersection. If there are not, we can simply return an empty list.

Otherwise, we can initialize an empty list to store the order in which vehicles should proceed. We can then check if there are any vehicles already in the intersection. If so, we should continue to allow them to complete their movements.

If there are not any vehicles in the intersection, we can check the state of the traffic lights. If the lights on road 2 are green, we can allow vehicles from road 2 to proceed in the order they arrived at the intersection. We can dequeue each vehicle from the queue for road 2 and add its direction to the list of directions in which vehicles should proceed.

If there are no more vehicles waiting on road 2, or if the lights on road 2 are not green, we can move to the next step. We can check whether the lights on road 1 are green. If they are, we can dequeue each vehicle from the queue for road 1, in the order they arrived at the intersection, and add their direction to the list of directions in which vehicles should proceed.

Once all vehicles that can proceed have been dequeued, we can return the list of directions in which vehicles should proceed.

Here's the implementation of the function in Python:

from collections import deque

def trafficIntersection(N, vehicles):
    # Initialize queues for each road
    q1 = deque()
    q2 = deque()

    # Initialize the traffic light state
    lights = {'road1': 'red', 'road2': 'green'}

    # Add each vehicle to the appropriate queue
    for vehicle in vehicles:
        time, road, direction = vehicle
        if road == 1:
            q1.append((time, direction))
        else:
            q2.append((time, direction))

    # Define a function to process the intersection
    def processIntersection():
        # If no vehicles are waiting, return an empty list
        if not q1 and not q2:
            return []

        # Initialize an empty list to store the order in which vehicles should proceed
        order = []

        # If there are any vehicles already in the intersection, allow them to proceed
        if lights['road1'] == 'green' and q1:
            order.append(q1.popleft()[1])
        elif lights['road2'] == 'green' and q2:
            order.append(q2.popleft()[1])
        else:
            return []

        # Allow vehicles waiting on road 2 to proceed in the order they arrived at the intersection
        while lights['road2'] == 'green' and q2:
            order.append(q2.popleft()[1])

        # Allow vehicles waiting on road 1 to proceed in the order they arrived at the intersection
        while lights['road1'] == 'green' and q1:
            order.append(q1.popleft()[1])

        # Update the state of the traffic lights
        if lights['road1'] == 'red' and not q1:
            lights['road1'] = 'green'
            lights['road2'] = 'red'
        elif lights['road2'] == 'red' and not q2:
            lights['road2'] = 'green'
            lights['road1'] = 'red'

        # Return the order in which vehicles should proceed
        return order

    # Process the intersection and store the result for each time step
    result = []
    for i in range(201):
        result.append(processIntersection())

    # Flatten the result into a single list of directions
    return [direction for time in result for direction in time]

The function first initializes the queues and the traffic light state. It then loops through the list of vehicles waiting at the intersection and adds them to the appropriate queue.

The processIntersection function is then defined. This function checks whether there are any vehicles waiting at the intersection, and allows them to proceed in the order dictated by the problem statement. It returns a list of directions in which vehicles should proceed.

The trafficIntersection function then loops through 201 time steps and calls the processIntersection function at each time step. It stores the result for each time step in a list, and then flattens this list into a single list of directions.

Finally, the function returns this list of directions.

Traffic Light Controlled Intersection Solution Code

1