Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: The Latest Time to Catch a Bus

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "2332" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

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: If capacity or fewer passengers are waiting for a bus, they will all get on the bus, or The capacity passengers with the earliest arrival times will get on the bus. 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: Suppose you arrive at time 16. At time 10, the first bus departs with the 0th passenger. At time 20, the second bus departs with you and the 1st passenger. Note that you may not arrive at the same time as another passenger, which is why you must arrive before the 1st 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: Suppose you arrive at time 20. At time 10, the first bus departs with the 3rd passenger. At time 20, the second bus departs with the 5th and 1st passengers. At time 30, the third bus departs with the 0th passenger and you. Notice if you had arrived any later, then the 6th passenger would have taken your seat on the third bus. 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.

Explanation

Here's a breakdown of the solution approach, followed by the code:

  • Sort: Sort both the buses and passengers arrays. Sorting allows us to process buses and passengers in chronological order.
  • Simulate Boarding: Iterate through the sorted buses. For each bus, determine how many passengers can board from the sorted passengers list, considering the capacity.
  • Determine Arrival Time: After processing all buses, determine the latest possible arrival time for a new passenger. This involves checking if the last bus had remaining capacity or if the last boarded passenger allows a time slot right before them.

  • Time Complexity: O(n log n + m log m), where n is the number of buses and m is the number of passengers (due to sorting).

  • Space Complexity: O(1) (excluding space for input arrays), as we use only a constant amount of extra space.

Code

    def latestTimeCatchTheBus(buses, passengers, capacity):
    buses.sort()
    passengers.sort()

    passenger_idx = 0
    passengers_on_bus = 0
    last_bus_time = 0

    for bus_time in buses:
        passengers_on_bus = 0
        while passenger_idx < len(passengers) and passengers[passenger_idx] <= bus_time and passengers_on_bus < capacity:
            passenger_idx += 1
            passengers_on_bus += 1
        last_bus_time = bus_time

    if passengers_on_bus < capacity:
        arrival_time = last_bus_time
        while arrival_time in passengers:
            arrival_time -= 1
    else:
        arrival_time = passengers[passenger_idx - 1] - 1
        while arrival_time in passengers:
            arrival_time -= 1

    return arrival_time

More from this blog

C

Chatmagic blog

2894 posts