-
-
Notifications
You must be signed in to change notification settings - Fork 610
/
CarFleet.java
68 lines (58 loc) · 1.79 KB
/
CarFleet.java
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package problems.medium;
import java.util.Arrays;
import java.util.Comparator;
/**
* Why Did you create this class? what does it do?
*/
public class CarFleet {
public static void main(String[] args) {
System.out.println(new CarFleet().carFleet(10,
new int[] { 0, 2, 4 },
new int[] { 2, 3, 1 }));
System.out.println(new CarFleet().carFleet(10,
new int[] { 6, 8 },
new int[] { 3, 2 }));
System.out.println(new CarFleet().carFleet(12,
new int[] { 10, 8, 0, 5, 3 },
new int[] { 2, 4, 1, 1, 3 }));
}
public int carFleet(int target, int[] position, int[] speed) {
if (speed == null || speed.length == 0)
return 0;
if (speed.length == 1)
return 1;
int r = 0;
Pair[] pairs = new Pair[speed.length];
for (int i = 0; i < speed.length; i++) {
pairs[i] = new Pair(position[i], speed[i]);
}
Arrays.sort(pairs, Comparator.comparingInt(a -> a.pos));
double[] a = new double[pairs.length];
for (int i = pairs.length - 1; i >= 0; i--) {
a[i] = (target - pairs[i].pos * 1.0) / pairs[i].speed;
}
boolean infleet = false;
for (int i = a.length - 1; i > 0; i--) {
if (a[i] < a[i - 1]) {
r += infleet ? 0 : 1;
infleet = false;
continue;
}
if (!infleet) {
r++;
infleet = true;
}
a[i - 1] = Math.max(a[i - 1], a[i]);
}
r += (a[0] > a[1] ? 1 : 0);
return r;
}
class Pair {
int pos;
int speed;
Pair(int p, int s) {
pos = p;
speed = s;
}
}
}