Last active
January 15, 2024 01:01
-
-
Save smatthewenglish/84776176a22a7dd20b66633c7f7b4fa6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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