Tap here to switch tabs
Problem
Submissions

Problem: The Latest Time to Catch a Bus

med
30 min
Try to solve the The Latest Time to Catch a Bus problem.

Statement

You are given a 00 indexed integer array buses of length n, where buses[i] represents the departure time of the iextthi^{ ext{th}} bus. You are also given a 00 indexed integer array passengers of length m, where passengers[j] represents the arrival time of the jextthj^{ ext{th}} passenger. In addition, you are given an integer capacity, which denotes the maximum number of passengers that can board any single bus.

Passengers board buses in order of their arrival time. Each passenger will board the earliest departing bus that still has available capacity and departs at or after their arrival time.

You want to catch a bus. You are allowed to board a bus if there is remaining capacity at its departure time, but you cannot arrive at the same time as any other passenger.

Return the latest time you can arrive to catch a bus.

Note: You cannot arrive at the same time as another passenger, and you must arrive at or before the departure time of the bus you board.

Constraints:

  • n ==== buses.length

  • m ==== passengers.length

  • 1≤1 \leq n, m, capacity ≤105\leq 10^5

  • 2≤2 \leq buses[i], passengers[i] ≤109\leq 10^9

  • Each element in buses is unique.

  • Each element in passengers is unique.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: The Latest Time to Catch a Bus

med
30 min
Try to solve the The Latest Time to Catch a Bus problem.

Statement

You are given a 00 indexed integer array buses of length n, where buses[i] represents the departure time of the iextthi^{ ext{th}} bus. You are also given a 00 indexed integer array passengers of length m, where passengers[j] represents the arrival time of the jextthj^{ ext{th}} passenger. In addition, you are given an integer capacity, which denotes the maximum number of passengers that can board any single bus.

Passengers board buses in order of their arrival time. Each passenger will board the earliest departing bus that still has available capacity and departs at or after their arrival time.

You want to catch a bus. You are allowed to board a bus if there is remaining capacity at its departure time, but you cannot arrive at the same time as any other passenger.

Return the latest time you can arrive to catch a bus.

Note: You cannot arrive at the same time as another passenger, and you must arrive at or before the departure time of the bus you board.

Constraints:

  • n ==== buses.length

  • m ==== passengers.length

  • 1≤1 \leq n, m, capacity ≤105\leq 10^5

  • 2≤2 \leq buses[i], passengers[i] ≤109\leq 10^9

  • Each element in buses is unique.

  • Each element in passengers is unique.