Skip to content

Instantly share code, notes, and snippets.

@jalasem
Created August 2, 2021 08:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jalasem/75c500dd0857e4c9433616bbca883131 to your computer and use it in GitHub Desktop.
Save jalasem/75c500dd0857e4c9433616bbca883131 to your computer and use it in GitHub Desktop.
public static long carParkingRoof(List<Long> cars, int k) {
// Write your code here
if (cars.size() == 0 || k < 0) {
return 0;
}
Collections.sort(cars);
long minDist = Long.MAX_VALUE;
for (int i = 0; i <= cars.size() - k; i++) {
minDist = Math.min(minDist, cars.get(i + k - 1) - cars.get(i));
}
return minDist + 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment