Class Solution
-
- All Implemented Interfaces:
public final class Solution2332 - 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 the <code>i<sup>th</sup></code> bus. You are also given a 0-indexed integer arraypassengersof lengthm, wherepassengers[j]represents the arrival time of the <code>j<sup>th</sup></code> passenger. 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 1<sup>st</sup> bus departs with the 1<sup>st</sup> passenger.
The 2<sup>nd</sup> bus departs with you and the 2<sup>nd</sup> passenger.
Note that you must not arrive at the same time as the passengers, which is why you must arrive before the 2<sup>nd</sup> 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 1<sup>st</sup> bus departs with the 4<sup>th</sup> passenger.
The 2<sup>nd</sup> bus departs with the 6<sup>th</sup> and 2<sup>nd</sup> passengers.
The 3<sup>rd</sup> bus departs with the 1<sup>s</sup><sup>t</sup> passenger and you.
Constraints:
n == buses.lengthm == passengers.length<code>1 <= n, m, capacity <= 10<sup>5</sup></code>
<code>2 <= busesi, passengersi<= 10<sup>9</sup></code>
Each element in
busesis unique.Each element in
passengersis unique.
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final IntegerlatestTimeCatchTheBus(IntArray buses, IntArray passengers, Integer capacity)-
-
Method Detail
-
latestTimeCatchTheBus
final Integer latestTimeCatchTheBus(IntArray buses, IntArray passengers, Integer capacity)
-
-
-
-