2332. The Latest Time to Catch a Bus Medium

@problem@discussion
#Array#Two Pointers#Binary Search#Sorting



1/**
2 * [2332] The Latest Time to Catch a Bus
3 *
4 * You are given a 0-indexed integer array buses of length n, where buses[i] represents the departure time of the i^th bus. You are also given a 0-indexed integer array passengers of length m, where passengers[j] represents the arrival time of the j^th passenger. All bus departure times are unique. All passenger arrival times are unique.
5 * You are given an integer capacity, which represents the maximum number of passengers that can get on each bus.
6 * When a passenger arrives, they will wait in line for the next available bus. You can get on a bus that departs at x minutes if you arrive at y minutes where y <= x, and the bus is not full. Passengers with the earliest arrival times get on the bus first.
7 * More formally when a bus arrives, either:
8 * 
9 * 	If capacity or fewer passengers are waiting for a bus, they will all get on the bus, or
10 * 	The capacity passengers with the earliest arrival times will get on the bus.
11 * 
12 * Return the latest time you may arrive at the bus station to catch a bus. You cannot arrive at the same time as another passenger.
13 * Note: The arrays buses and passengers are not necessarily sorted.
14 *  
15 * Example 1:
16 * 
17 * Input: buses = [10,20], passengers = [2,17,18,19], capacity = 2
18 * Output: 16
19 * Explanation: Suppose you arrive at time 16.
20 * At time 10, the first bus departs with the 0^th passenger. 
21 * At time 20, the second bus departs with you and the 1^st passenger.
22 * Note that you may not arrive at the same time as another passenger, which is why you must arrive before the 1^st passenger to catch the bus.
23 * Example 2:
24 * 
25 * Input: buses = [20,30,10], passengers = [19,13,26,4,25,11,21], capacity = 2
26 * Output: 20
27 * Explanation: Suppose you arrive at time 20.
28 * At time 10, the first bus departs with the 3^rd passenger. 
29 * At time 20, the second bus departs with the 5^th and 1^st passengers.
30 * At time 30, the third bus departs with the 0^th passenger and you.
31 * Notice if you had arrived any later, then the 6^th passenger would have taken your seat on the third bus.
32 *  
33 * Constraints:
34 * 
35 * 	n == buses.length
36 * 	m == passengers.length
37 * 	1 <= n, m, capacity <= 10^5
38 * 	2 <= buses[i], passengers[i] <= 10^9
39 * 	Each element in buses is unique.
40 * 	Each element in passengers is unique.
41 * 
42 */
43pub struct Solution {}
44
45// problem: https://leetcode.com/problems/the-latest-time-to-catch-a-bus/
46// discuss: https://leetcode.com/problems/the-latest-time-to-catch-a-bus/discuss/?currentPage=1&orderBy=most_votes&query=
47
48// submission codes start here
49
50impl Solution {
51    pub fn latest_time_catch_the_bus(buses: Vec<i32>, passengers: Vec<i32>, capacity: i32) -> i32 {
52        0
53    }
54}
55
56// submission codes end
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    #[test]
63    fn test_2332() {
64    }
65}
66


Back
© 2025 bowen.ge All Rights Reserved.