Skip to content

Practice The Latest Time to Catch a Bus

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#2332
LeetCode Problem

The Latest Time to Catch a Bus

Problem Statement

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. When a passenger arrives, they will wait in line for the next available bus. You can get on a bus that departs 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. More formally when a bus arrives, either: 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.

Source: The Latest Time to Catch a Bus on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
buses = [10,20], passengers = [2,17,18,19], capacity = 2
Output
16

# Example 2

Input
buses = [20,30,10], passengers = [19,13,26,4,25,11,21], capacity = 2
Output
20

Constraints

n == buses.length
m == passengers.length
1 <= n, m, capacity <= 10^5
2 <= buses[i], passengers[i] <= 10^9
Each element in buses is unique.
Each element in passengers is unique.

Related LeetCode Problems

Practice more problems similar to The Latest Time to Catch a Bus.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.