Class Solution
- java.lang.Object
-
- g2301_2400.s2332_the_latest_time_to_catch_a_bus.Solution
-
public class Solution extends Object
2332 - The Latest Time to Catch a Bus.Medium
You are given a 0-indexed integer array
busesof lengthn, wherebuses[i]represents the departure time of theithbus. You are also given a 0-indexed integer arraypassengersof lengthm, wherepassengers[j]represents the arrival time of thejthpassenger. All bus departure times are unique. All passenger arrival times are unique.You are given an integer
capacity, which represents the maximum number of passengers that can get on each bus.The passengers will get on the next available bus. You can get on a bus that will depart at
xminutes if you arrive atyminutes wherey <= x, and the bus is not full. Passengers with the earliest arrival times get on the bus first.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.
Note: The arrays
busesandpassengersare not necessarily sorted.Example 1:
Input: buses = [10,20], passengers = [2,17,18,19], capacity = 2
Output: 16
Explanation:
The 1st bus departs with the 1st passenger.
The 2nd bus departs with you and the 2nd passenger.
Note that you must not arrive at the same time as the passengers, which is why you must arrive before the 2nd passenger to catch the bus.
Example 2:
Input: buses = [20,30,10], passengers = [19,13,26,4,25,11,21], capacity = 2
Output: 20
Explanation:
The 1st bus departs with the 4th passenger.
The 2nd bus departs with the 6th and 2nd passengers.
The 3rd bus departs with the 1st passenger and you.
Constraints:
n == buses.lengthm == passengers.length1 <= n, m, capacity <= 1052 <= buses[i], passengers[i] <= 109- Each element in
busesis unique. - Each element in
passengersis unique.
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intlatestTimeCatchTheBus(int[] buses, int[] passengers, int capacity)
-