package leetcode;

/**
 * https://leetcode.com/problems/car-fleet/
 */
public class Problem853 {
    public int carFleet(int target, int[] position, int[] speed) {
        // TODO
        return 0;
    }

    public static void main(String[] args) {
        Problem853 prob = new Problem853();
//        System.out.println(prob.carFleet(12, new int[]{10, 8, 0, 5, 3}, new int[]{2, 4, 1, 1, 3})); // 3
//        System.out.println(prob.carFleet(12, new int[]{10, 7, 0, 5, 3}, new int[]{2, 4, 1, 1, 3})); // 4
//        System.out.println(prob.carFleet(12, new int[]{1, 2, 3, 4, 5}, new int[]{5, 4, 3, 2, 1})); // 1
//        System.out.println(prob.carFleet(15, new int[]{4, 5}, new int[]{2, 1})); // 1
//        System.out.println(prob.carFleet(10, new int[]{2, 4}, new int[]{3, 2})); // 1
        System.out.println(prob.carFleet(31, new int[]{5, 26, 18, 25, 29, 21, 22, 12, 19, 6}, new int[]{7, 6, 6, 4, 3, 4, 9, 7, 6, 4})); // 6
    }
}

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;
    }
}

Solve problem 853