The Latest Time to Catch a Bus - Complete Solution Guide
The Latest Time to Catch a Bus is LeetCode problem 2332, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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. You are given an integer capacity , which represents the maximum number of passengers that can get on each bus. When a passenger arrives, they will wait in line
Detailed Explanation
The problem describes a scenario where buses depart at specific times (`buses`) and passengers arrive at the bus station at specific times (`passengers`). Each bus has a limited capacity (`capacity`). The goal is to determine the latest possible arrival time at the bus station that guarantees you will be able to catch a bus, without arriving at the same time as any other passenger. Passengers are served in order of their arrival time, and a passenger can board a bus if their arrival time is less than or equal to the bus departure time, and the bus has available capacity.
Solution Approach
The solution simulates the bus boarding process. First, both the `buses` and `passengers` arrays are sorted. Then, the code iterates through the sorted `buses` array. For each bus, it iterates through the `passengers` array, keeping track of the index of the next passenger to board (`p_idx`). If a passenger's arrival time is less than or equal to the bus departure time and the bus has capacity, the passenger is considered to have boarded the bus. The number of passengers boarding the last bus is stored. Finally, the latest possible arrival time is determined based on whether the last bus had free capacity or not. If the last bus had capacity, the latest arrival time is the departure time of the last bus. Otherwise, it's just before the arrival time of the last passenger who boarded. The code then checks if the determined time conflicts with any existing passenger arrival times, decrementing the time if needed to avoid collisions.
Step-by-Step Algorithm
- Step 1: Sort the `buses` array in ascending order.
- Step 2: Sort the `passengers` array in ascending order.
- Step 3: Create a set `passenger_times` to efficiently check if a potential arrival time is already taken by a passenger.
- Step 4: Initialize `p_idx` to 0 to keep track of the current passenger being considered.
- Step 5: Initialize `boarded_on_last_bus` to 0 to store the number of passengers who boarded the last bus.
- Step 6: Iterate through the sorted `buses` array.
- Step 7: For each bus, iterate through the `passengers` array from the current `p_idx`, checking if a passenger can board the bus (arrival time <= bus time and bus capacity not full).
- Step 8: Update `p_idx` and the number of passengers boarded on the current bus.
- Step 9: Update `boarded_on_last_bus` with the number of passengers boarded on the current bus.
- Step 10: After processing all buses, determine the `latest_time`. If the last bus had capacity, the `latest_time` is the departure time of the last bus. Otherwise, it's just before the arrival time of the last passenger who boarded.
- Step 11: Check if the `latest_time` conflicts with any existing passenger arrival times. If it does, decrement the `latest_time` until it doesn't conflict.
- Step 12: Return the `latest_time`.
Key Insights
- Insight 1: Sorting both `buses` and `passengers` arrays is crucial for efficiently determining which passengers can board which buses based on arrival time.
- Insight 2: Iterating through the sorted `buses` and keeping track of the number of boarded passengers helps to simulate the boarding process and determine the number of passengers that boarded the last bus.
- Insight 3: The latest possible arrival time is either the departure time of the last bus if there's still capacity, or just before the arrival time of the last passenger who boarded a bus.
Complexity Analysis
Time Complexity: O(n log n + m log m)
Space Complexity: O(m)
Topics
This problem involves: Array, Two Pointers, Binary Search, Sorting.
Companies
Asked at: Zoho.