Skip to content

Latest commit

 

History

History
39 lines (31 loc) · 1.07 KB

Question_1279.md

File metadata and controls

39 lines (31 loc) · 1.07 KB

LeetCode Records - Question 1279 Traffic Light Controlled Intersection

Attempt 1: Use a LinkedBlockingQueue to track car IDs

class TrafficLight {

    private int roadIdIsGreen;
    private BlockingQueue<Integer> queue;

    public TrafficLight() {
        roadIdIsGreen = 1;
        queue = new LinkedBlockingQueue();
    }
    
    public 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
        Runnable turnGreen,  // Use turnGreen.run() to turn light to green on current road
        Runnable crossCar    // Use crossCar.run() to make car cross the intersection 
    ) {
        queue.add(carId);

        while (queue.peek() != carId) {}

        if (roadIdIsGreen != roadId) {
            roadIdIsGreen = roadId;
            turnGreen.run();
        }
        crossCar.run();

        queue.poll();
    }
}
  • Runtime: 11 ms (Beats: 97.70%)
  • Memory: 43.46 MB (Beats: 70.11%)