3829. Design Ride Sharing System Medium

@problem@discussion
#Hash Table#Design#Queue#Data Stream



1/**
2 * [3829] Design Ride Sharing System
3 *
4 * 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.
5 * Implement the RideSharingSystem class:
6 * 
7 * 	RideSharingSystem() Initializes the system.
8 * 	void addRider(int riderId) Adds a new rider with the given riderId.
9 * 	void addDriver(int driverId) Adds a new driver with the given driverId.
10 * 	int[] matchDriverWithRider() Matches the earliest available driver with the earliest waiting rider and removes both of them from the system. Returns an integer array of size 2 where result = [driverId, riderId] if a match is made. If no match is available, returns [-1, -1].
11 * 	void cancelRider(int riderId) Cancels the ride request of the rider with the given riderId if the rider exists and has not yet been matched.
12 * 
13 *  
14 * <strong class="example">Example 1:
15 * <div class="example-block">
16 * Input:<br />
17 * <span class="example-io">["RideSharingSystem", "addRider", "addDriver", "addRider", "matchDriverWithRider", "addDriver", "cancelRider", "matchDriverWithRider", "matchDriverWithRider"]<br />
18 * [[], [3], [2], [1], [], [5], [3], [], []]</span>
19 * Output:<br />
20 * <span class="example-io">[null, null, null, null, [2, 3], null, null, [5, 1], [-1, -1]] </span>
21 * Explanation
22 * RideSharingSystem rideSharingSystem = new RideSharingSystem(); // Initializes the system<br />
23 * rideSharingSystem.addRider(3); // rider 3 joins the queue<br />
24 * rideSharingSystem.addDriver(2); // driver 2 joins the queue<br />
25 * rideSharingSystem.addRider(1); // rider 1 joins the queue<br />
26 * rideSharingSystem.matchDriverWithRider(); // returns [2, 3]<br />
27 * rideSharingSystem.addDriver(5); // driver 5 becomes available<br />
28 * rideSharingSystem.cancelRider(3); // rider 3 is already matched, cancel has no effect<br />
29 * rideSharingSystem.matchDriverWithRider(); // returns [5, 1]<br />
30 * rideSharingSystem.matchDriverWithRider(); // returns [-1, -1]</div>
31 * <strong class="example">Example 2:
32 * <div class="example-block">
33 * Input:<br />
34 * <span class="example-io">["RideSharingSystem", "addRider", "addDriver", "addDriver", "matchDriverWithRider", "addRider", "cancelRider", "matchDriverWithRider"]<br />
35 * [[], [8], [8], [6], [], [2], [2], []]</span>
36 * Output:<br />
37 * <span class="example-io">[null, null, null, null, [8, 8], null, null, [-1, -1]] </span>
38 * Explanation
39 * RideSharingSystem rideSharingSystem = new RideSharingSystem(); // Initializes the system<br />
40 * rideSharingSystem.addRider(8); // rider 8 joins the queue<br />
41 * rideSharingSystem.addDriver(8); // driver 8 joins the queue<br />
42 * rideSharingSystem.addDriver(6); // driver 6 joins the queue<br />
43 * rideSharingSystem.matchDriverWithRider(); // returns [8, 8]<br />
44 * rideSharingSystem.addRider(2); // rider 2 joins the queue<br />
45 * rideSharingSystem.cancelRider(2); // rider 2 cancels<br />
46 * rideSharingSystem.matchDriverWithRider(); // returns [-1, -1]</div>
47 *  
48 * Constraints:
49 * 
50 * 	1 <= riderId, driverId <= 1000
51 * 	Each riderId is unique among riders and is added at most once.
52 * 	Each driverId is unique among drivers and is added at most once.
53 * 	At most 1000 calls will be made in total to addRider​​​​​​​, addDriver, matchDriverWithRider, and cancelRider.
54 * 
55 */
56pub struct Solution {}
57
58// problem: https://leetcode.com/problems/design-ride-sharing-system/
59// discuss: https://leetcode.com/problems/design-ride-sharing-system/discuss/?currentPage=1&orderBy=most_votes&query=
60
61// submission codes start here
62
63struct RideSharingSystem {
64        false
65    }
66
67
68/** 
69 * `&self` means the method takes an immutable reference.
70 * If you need a mutable reference, change it to `&mut self` instead.
71 */
72impl RideSharingSystem {
73
74    fn new() -> Self {
75        
76    }
77    
78    fn add_rider(&self, rider_id: i32) {
79        
80    }
81    
82    fn add_driver(&self, driver_id: i32) {
83        
84    }
85    
86    fn match_driver_with_rider(&self) -> Vec<i32> {
87        
88    }
89    
90    fn cancel_rider(&self, rider_id: i32) {
91        
92    }
93}
94
95/**
96 * Your RideSharingSystem object will be instantiated and called as such:
97 * let obj = RideSharingSystem::new();
98 * obj.add_rider(riderId);
99 * obj.add_driver(driverId);
100 * let ret_3: Vec<i32> = obj.match_driver_with_rider();
101 * obj.cancel_rider(riderId);
102 */
103
104// submission codes end
105
106#[cfg(test)]
107mod tests {
108    use super::*;
109
110    #[test]
111    fn test_3829() {
112    }
113}
114

Back
© 2026 bowen.ge All Rights Reserved.