MCPcopy Index your code
hub / github.com/neetcode-gh/leetcode / carFleet

Method carFleet

java/0853-car-fleet.java:3–23  ·  view source on GitHub ↗
(int target, int[] position, int[] speed)

Source from the content-addressed store, hash-verified

1class 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}

Callers

nothing calls this directly

Calls 4

isEmptyMethod · 0.45
peekMethod · 0.45
pushMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected