**Physical Address**

304 North Cardinal St.

Dorchester Center, MA 02124

You are given a **0-indexed** integer array `buses`

of length `n`

, where `buses[i]`

represents the departure time of the `i`

bus. You are also given a ^{th}**0-indexed** integer array `passengers`

of length `m`

, where `passengers[j]`

represents the arrival time of the `j`

passenger. All bus departure times are unique. All passenger arrival times are unique.^{th}

You are given an integer `capacity`

, which represents the **maximum** number of passengers that can get on each bus.

The passengers will get on the next available bus. You can get on a bus that will depart 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.

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:
The 1st bus departs with the 1st passenger.
The 2nd bus departs with you and the 2nd passenger.
Note that you must not arrive at the same time as the passengers, which is why you must arrive before the 2nd 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:
The 1st bus departs with the 4th passenger.
The 2nd bus departs with the 6th and 2nd passengers.
The 3rd bus departs with the 1st passenger and you.
```

**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**.

```
class Solution {
public:
int latestTimeCatchTheBus(vector<int>& buses, vector<int>& passengers, int capacity) {
queue<int> q;
sort(buses.begin(),buses.end());
sort(passengers.begin(),passengers.end());
int n=buses.size();
int m=passengers.size();
set<int> st;
for(auto p:passengers)
{
q.push(p);
st.insert(p);
}
int ans=0;
for(int i=0;i<n;i++)
{
int currbus=buses[i]; // curr bus depature time.
int count=0; //number of people in curr bus
int x;
//CASE1
while(!q.empty() && count<capacity && q.front()<=currbus)
{
x=q.front();
q.pop();
if(st.find(x-1)==st.end()) //checking if person timing-1 doesnt exist and update the answer.
ans=x-1;
count++;
}
//CASE2
if(count<capacity)
{
while(st.find(currbus)!=st.end()) //starting from dept time find a time which does not exist int the set already.
{
currbus--;
}
ans=max(ans,currbus);
}
}
return ans;
}
};
```

```
public int latestTimeCatchTheBus(int[] buses, int[] passengers, int capacity) {
Arrays.sort(buses);
Arrays.sort(passengers);
Set<Integer> set = new HashSet<>();
int ans=0,j=0;
for(int i=0;i<buses.length;i++){
int c = 0;
while(j<passengers.length && c<capacity && passengers[j]<=buses[i]){
if(!set.contains(passengers[j]-1)){
ans=passengers[j]-1;
}
set.add(passengers[j]);
j++; c++;
}
if(c<capacity && !set.contains(buses[i])){
ans = buses[i];
}
}
return ans;
}
```

```
class Solution:
def latestTimeCatchTheBus(self, buses: List[int], passengers: List[int], capacity: int) -> int:
passengers.sort()
cur = 0
for time in sorted(buses):
cap = capacity
while cur < len(passengers) and passengers[cur] <= time and cap > 0:
cur += 1
cap -= 1
best = time if cap > 0 else passengers[cur - 1]
passengers = set(passengers)
while best in passengers:
best -= 1
return best
```

In our experience, we suggest you solve this The Latest Time to Catch a Bus LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the The Latest Time to Catch a Bus LeetCode Solution

I hope this The Latest Time to Catch a Bus LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

**More Coding Solutions >>**