package leetcode;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

/**
 * https://leetcode.com/problems/car-fleet/
 */
public class Problem853 {
    public int carFleet(int target, int[] position, int[] speed) {
        TreeMap<Integer, Double> map = new TreeMap<>();
        for (int i = 0; i < position.length; ++i) {
            map.put(position[i], (double) (target - position[i]) / speed[i]);
        }
        double maxSoFar = 0;
        int answer = 0;
        List<Double> timeNeeded = new ArrayList<>(map.values());
        for (int i = position.length - 1; i >= 0; i--) {
            if (timeNeeded.get(i) > maxSoFar) {
                maxSoFar = timeNeeded.get(i);
                answer++;
            }
        }
        return answer;
    }
}

@@ -34,20 +34,17 @@
 il.Array
-List
+s
 ;%0Aimport
@@ -58,38 +58,18 @@
 til.
-List;%0Aimport java.util.TreeMap
+Comparator
 ;%0A%0A/
@@ -224,54 +224,113 @@
     
-TreeMap%3CInteger, Double%3E map = new TreeMap%3C%3E()
+if (position.length == 0) %7B%0A            return 0;%0A        %7D%0A        Car%5B%5D cars = new Car%5Bposition.length%5D
 ;%0A  
@@ -395,15 +395,25 @@
     
-map.put
+cars%5Bi%5D = new Car
 (pos
@@ -489,116 +489,91 @@
     
-double maxSoFar = 0;%0A        int answer = 0;%0A        List%3CDouble%3E timeNeeded = new ArrayList%3C%3E(map.values())
+Arrays.sort(cars, Comparator.comparingInt(a -%3E a.position));%0A        int answer = 1
 ;%0A  
@@ -591,24 +591,20 @@
 int i = 
-position
+cars
 .length 
@@ -611,17 +611,16 @@
 - 1; i %3E
-=
  0; i--)
@@ -642,158 +642,411 @@
 if (
-timeNeeded.get(i) %3E maxSoFar) %7B%0A                maxSoFar = timeNeeded.get(i);%0A               
+cars%5Bi%5D.time %3C cars%5Bi - 1%5D.time) %7B%0A                answer++;%0A            %7D else %7B%0A                cars%5Bi - 1%5D = cars%5Bi%5D;%0A            %7D%0A        %7D%0A        return
  answer
-++
 ;%0A    
+%7D%0A%0A
     
-    %7D%0A        %7D%0A        return answer;
+private static class Car %7B%0A        private final int position;%0A        private final double time;%0A%0A        public Car(int position, double time) %7B%0A            this.position = position;%0A            this.time = time;%0A        %7D
 %0A   

Update problem 853