[Solved] The Latest Time to Catch a Bus LeetCode Contest

The Latest Time to Catch a Bus: You are given a 0-indexed integer array buses of length n, where buses[i] represents the departure time of the ith bus. You are also given a 0-indexed integer array passengers of length m, where passengers[j] represents the arrival time of the jth 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 <= 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 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.length
  • m == passengers.length
  • 1 <= n, m, capacity <= 105
  • 2 <= buses[i], passengers[i] <= 109
  • Each element in buses is unique.
  • Each element in passengers is unique.

Solution

int latestTimeCatchTheBus(vector<int>& b, vector<int>& p, int cap) {
    sort(begin(b), end(b));
    sort(begin(p), end(p));
    int i = 0, j = 0, start = 0;
    while (i < b.size() - 1 && j < p.size())
        if(p[j] <= b[i] && j - start < cap)
            ++j;
        else {
            ++i;
            start = j;
        }
    while (j < p.size() && p[j] <= b.back() && cap) {
        ++j;
        --cap;
    }
    int latest = cap ? b.back() : p[j - 1];
    for (j = j - 1; j >= 0 && latest == p[j]; --j)
        latest = p[j] - 1;
    return latest;
}
public int latestTimeCatchTheBus(int[] buses, int[] passengers, int capacity) {
    Arrays.sort(passengers);
    Arrays.sort(buses);
    if(passengers[0] > buses[buses.length - 1]) return buses[buses.length - 1];
    int result = passengers[0] - 1;
    int i = 0, j = 0;
    while(i < buses.length){
        int occupiedSeats = 0;
        while(occupiedSeats < capacity && j < passengers.length && passengers[j] <= buses[i]){
            if(j > 0 && passengers[j] - passengers[j-1] != 1){
                result = passengers[j] - 1;
            }
            j++;
            occupiedSeats++;
        }
        if(j > 0 && occupiedSeats < capacity && buses[i] != passengers[j-1]){
            result = buses[i];
        }
        i++;
    }
    return result;
}
class Solution:
    def latestTimeCatchTheBus(self, buses: List[int], passengers: List[int], capacity: int) -> int:
        buses.sort()
        passengers.sort()
        m = len(buses)
        n = len(passengers)
        
        ans = 1
        c = 0
        j = 0
        for i in range(m):
            while j < n and c < capacity and passengers[j] <= buses[i]:
                if passengers[j] - 1 != passengers[j - 1]:
                    ans = passengers[j] - 1
                c += 1
                j += 1
            if c < capacity and (j == 0 or passengers[j - 1] < buses[i]):
                ans = buses[i]
            c = 0
        return ans

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *