LeetCode in Kotlin

3829. Design Ride Sharing System

Medium

A ride sharing system manages ride requests from riders and availability from drivers. Riders request rides, and drivers become available over time. The system should match riders and drivers in the order they arrive.

Implement the RideSharingSystem class:

Example 1:

Input:
[“RideSharingSystem”, “addRider”, “addDriver”, “addRider”, “matchDriverWithRider”, “addDriver”, “cancelRider”, “matchDriverWithRider”, “matchDriverWithRider”]
[[], [3], [2], [1], [], [5], [3], [], []]

Output:
[null, null, null, null, [2, 3], null, null, [5, 1], [-1, -1]]

Explanation

RideSharingSystem rideSharingSystem = new RideSharingSystem(); // Initializes the system
rideSharingSystem.addRider(3); // rider 3 joins the queue
rideSharingSystem.addDriver(2); // driver 2 joins the queue
rideSharingSystem.addRider(1); // rider 1 joins the queue
rideSharingSystem.matchDriverWithRider(); // returns [2, 3]
rideSharingSystem.addDriver(5); // driver 5 becomes available
rideSharingSystem.cancelRider(3); // rider 3 is already matched, cancel has no effect
rideSharingSystem.matchDriverWithRider(); // returns [5, 1]
rideSharingSystem.matchDriverWithRider(); // returns [-1, -1]

Example 2:

Input:
[“RideSharingSystem”, “addRider”, “addDriver”, “addDriver”, “matchDriverWithRider”, “addRider”, “cancelRider”, “matchDriverWithRider”]
[[], [8], [8], [6], [], [2], [2], []]

Output:
[null, null, null, null, [8, 8], null, null, [-1, -1]]

Explanation

RideSharingSystem rideSharingSystem = new RideSharingSystem(); // Initializes the system
rideSharingSystem.addRider(8); // rider 8 joins the queue
rideSharingSystem.addDriver(8); // driver 8 joins the queue
rideSharingSystem.addDriver(6); // driver 6 joins the queue
rideSharingSystem.matchDriverWithRider(); // returns [8, 8]
rideSharingSystem.addRider(2); // rider 2 joins the queue
rideSharingSystem.cancelRider(2); // rider 2 cancels
rideSharingSystem.matchDriverWithRider(); // returns [-1, -1]

Constraints:

Solution

import java.util.LinkedList
import java.util.Queue

class RideSharingSystem {
    var rider: Queue<Int> = LinkedList<Int>()
    var driver: Queue<Int> = LinkedList<Int>()
    var cancel: MutableMap<Int?, Int?> = HashMap<Int?, Int?>()

    fun addRider(riderId: Int) {
        if (cancel.containsKey(riderId)) {
            cancel.remove(riderId)
        }
        rider.add(riderId)
    }

    fun addDriver(driverId: Int) {
        driver.add(driverId)
    }

    fun matchDriverWithRider(): IntArray? {
        while (rider.isNotEmpty() && cancel.containsKey(rider.peek())) {
            rider.poll()
        }
        if (rider.isNotEmpty() && driver.isNotEmpty()) {
            val ans = intArrayOf(driver.peek()!!, rider.peek()!!)
            rider.poll()
            driver.poll()
            return ans
        }
        return intArrayOf(-1, -1)
    }

    fun cancelRider(riderId: Int) {
        cancel[riderId] = 1
    }
}

/*
 * Your RideSharingSystem object will be instantiated and called as such:
 * RideSharingSystem obj = new RideSharingSystem();
 * obj.addRider(riderId);
 * obj.addDriver(driverId);
 * int[] param_3 = obj.matchDriverWithRider();
 * obj.cancelRider(riderId);
 */