1
class Solution {
2
public int latestTimeCatchTheBus(int[] buses, int[] passengers, int capacity) {
3
Arrays.sort(buses);
4
Arrays.sort(passengers);
5
HashSet<Integer> set = new HashSet<>();
6
for (int val : passengers) {
7
set.add(val);
8
}
9
int n = buses.length;
10
int m = passengers.length;
11
int solb = capacity; // solb = space on last bus
12
int lastPerson = 0;
13
int i = 0, j = 0;
14
while (i < n && j < m) {
15
int cc = capacity; // cc => current capacity;
16
while (j < m && cc > 0 && buses[i] >= passengers[j]) {
17
cc--;
18
lastPerson = passengers[j];
19
j++;
20
}
21
i++;
22
solb = cc;
23
}
24
int x = lastPerson;
25
if (solb > 0 || i != n) {
26
x = buses[n - 1];
27
}
28
while (set.contains(x) == true) {
29
x--;
30
}
31
return x;
32
}
33
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0