Leetcode Problem 2332. The Latest Time to Catch a Bus
2332. The Latest Time to Catch a Bus
Leetcode Solutions
Greedy Approach to Find the Latest Time to Catch a Bus
Sort the buses and passengers arrays.
Initialize variables to track the current bus index, passenger index, and the current capacity of the bus.
Iterate over the buses, for each bus:
a. Reset the current capacity to 0.
b. Board passengers until the bus is full or no more passengers can board.
c. If processing the last bus, determine the latest time to arrive:
i. If the bus is not full, the latest time is the bus's departure time or the latest time not clashing with passengers.
ii. If the bus is full, the latest time is one minute before the last passenger's arrival time, ensuring no clash.
Return the calculated latest time.
Iterative Approach with Time Set to Find the Latest Time to Catch a Bus