Skip to content

Instantly share code, notes, and snippets.

@smatthewenglish
Last active January 15, 2024 01:01
Show Gist options
  • Save smatthewenglish/84776176a22a7dd20b66633c7f7b4fa6 to your computer and use it in GitHub Desktop.
Save smatthewenglish/84776176a22a7dd20b66633c7f7b4fa6 to your computer and use it in GitHub Desktop.
/**
* The time complexity of the code is O(n^2), due to the nested stream operation.
*/
class Solution {
@Test
void test() {
List<Integer> departures = new ArrayList<>(Arrays.asList(26, 19, 19, 4, 4));
List<Integer> returns = new ArrayList<>(Arrays.asList(29, 26, 28, 19, 25));
Collections.sort(departures);
int vansInFleet = 0;
for(int i = 0; i < departures.size(); i++) {
int timeDepart = departures.get(i);
int arrivedThusFar = returns.stream()
.filter(x -> x <= timeDepart)
.collect(Collectors.toList()).size();
if(vansInFleet + arrivedThusFar <= i) {
//we don't have a van
vansInFleet++;
}
}
assertEquals(3, vansInFleet);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment