@@ -0,0 +1,1851 @@
+package org.pilgrim.leetcode.y2022;%0D%0A%0D%0Aimport java.util.Arrays;%0D%0A%0D%0A/**%0D%0A *%0D%0A * There are n cars going to the same destination along a one-lane road. The%0D%0A * destination is target miles away. You are given two integer array position%0D%0A * and speed, both of length n, where position%5Bi%5D is the position of the ith car%0D%0A * and speed%5Bi%5D is the speed of the ith car (in miles per hour). A car can never%0D%0A * pass another car ahead of it, but it can catch up to it and drive bumper to%0D%0A * bumper at the same speed. The faster car will slow down to match the slower%0D%0A * car's speed. The distance between these two cars is ignored (i.e., they are%0D%0A * assumed to have the same position). A car fleet is some non-empty set of cars%0D%0A * driving at the same position and same speed. Note that a single car is also a%0D%0A * car fleet. If a car catches up to a car fleet right at the destination point,%0D%0A * it will still be considered as one car fleet. Return the number of car fleets%0D%0A * that will arrive at the destination.%0D%0A *%0D%0A */%0D%0Apublic class CarFleet %7B%0D%0A%0D%0A%09public static void main(String%5B%5D args) %7B%0D%0A%09%09// TODO Auto-generated method stub%0D%0A%09%7D%0D%0A%0D%0A%09public int carFleet(int target, int%5B%5D position, int%5B%5D speed) %7B%0D%0A%09%09int fleet = 0;%0D%0A%0D%0A%09%09// Positions to time to destination%0D%0A%09%09double%5B%5D%5B%5D cars = new double%5Bposition.length%5D%5B2%5D;%0D%0A%0D%0A%09%09for (int i = 0; i %3C position.length; i++) %7B%0D%0A%09%09%09double time_to_dest = (double) (target - position%5Bi%5D) / speed%5Bi%5D;%0D%0A%09%09%09cars%5Bi%5D = new double%5B%5D %7B position%5Bi%5D, time_to_dest %7D;%0D%0A%09%09%7D%0D%0A%0D%0A%09%09// Sort by position ascending order%0D%0A%09%09Arrays.sort(cars, (a, b) -%3E Double.compare(a%5B0%5D, b%5B0%5D));%0D%0A%0D%0A%09%09double current_time = 0;%0D%0A%09%09for (int i = cars.length - 1; i %3E= 0; i--) %7B%0D%0A%09%09%09%0D%0A%09%09%09// Count intersections by time and count them as fleet%0D%0A%09%09%09if (cars%5Bi%5D%5B1%5D %3E current_time) %7B%0D%0A%09%09%09%09fleet++;%0D%0A%09%09%09%09current_time = cars%5Bi%5D%5B1%5D;%0D%0A%09%09%09%7D%0D%0A%09%09%7D%0D%0A%0D%0A%09%09return fleet;%0D%0A%09%7D%0D%0A%7D%0D%0A

add fleet car problem