Class Solution

  • All Implemented Interfaces:

    
    public final class Solution
    
                        

    2332 - The Latest Time to Catch a Bus\.

    Medium

    You are given a 0-indexed integer array buses of length n, where buses[i] represents the departure time of the <code>i<sup>th</sup></code> bus. You are also given a 0-indexed integer array passengers of length m, where passengers[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 x minutes if you arrive at y minutes where y &lt;= 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 buses and passengers are 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.length

    • m == passengers.length

    • <code>1 <= n, m, capacity <= 10<sup>5</sup></code>

    • <code>2 <= busesi, passengersi<= 10<sup>9</sup></code>

    • Each element in buses is unique.

    • Each element in passengers is unique.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
      Solution()
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      final Integer latestTimeCatchTheBus(IntArray buses, IntArray passengers, Integer capacity)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait