-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCarFleet.java
More file actions
50 lines (43 loc) · 1.76 KB
/
Copy pathCarFleet.java
File metadata and controls
50 lines (43 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class Solution {
@SuppressWarnings("unused")
public int carFleetMyAttempt(int target, int[] position, int[] speed) { // Failed because the overtake might happen anywhere on the track, not just to the fastest car at a given moment
// Create a fleet int equal to position.length
int fleet = position.length;
// Create a pointer
int i = 0;
// Create furthest position variable and save the index of a car
int furthestPosition = 0;
for (int j = 0; j < position.length; j++) {
if (position[j] > furthestPosition)
furthestPosition = position[j];
}
// Create a while loop
while (true) {
for (int j = 0; j < position.length; j++) {
position[j] += speed[j];
if (position[j] >= furthestPosition) {
}
}
}
// In each iteraton update the positions by speed
// If the furthest position changes, decrement the fleet variable;
// Update the car at position of pointer, make it the position of a saved car, and at saved car's position, and change their speed to 1
// Do the above untill all cars reach the finish line
}
public int carFleet(int target, int[] position, int[] speed) {
int fleet = 0;
double[] timeArray = new double[target];
for (int i = 0; i < position.length; i++) {
timeArray[position[i]] = (double) (target - position[i]) / speed[i];
}
double prev = 0.0;
for (int i = target-1; i >= 0 ; i--) {
double curr = timeArray[i];
if (curr > prev) {
prev = curr;
fleet++;
}
}
return fleet;
}
}