| 1 | class Solution { |
| 2 | |
| 3 | public int carFleet(int target, int[] position, int[] speed) { |
| 4 | if (position.length == 1) return 1; |
| 5 | Stack<Double> stack = new Stack<>(); |
| 6 | int[][] combine = new int[position.length][2]; |
| 7 | for (int i = 0; i < position.length; i++) { |
| 8 | combine[i][0] = position[i]; |
| 9 | combine[i][1] = speed[i]; |
| 10 | } |
| 11 | |
| 12 | Arrays.sort(combine, java.util.Comparator.comparingInt(o -> o[0])); |
| 13 | for (int i = combine.length - 1; i >= 0; i--) { |
| 14 | double currentTime = (double) (target - combine[i][0]) / |
| 15 | combine[i][1]; |
| 16 | if (!stack.isEmpty() && currentTime <= stack.peek()) { |
| 17 | continue; |
| 18 | } else { |
| 19 | stack.push(currentTime); |
| 20 | } |
| 21 | } |
| 22 | return stack.size(); |
| 23 | } |
| 24 | } |