-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxCarsCh6App.java
More file actions
55 lines (42 loc) · 1.38 KB
/
MaxCarsCh6App.java
File metadata and controls
55 lines (42 loc) · 1.38 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
51
52
53
54
55
package gr.aueb.cf.ch10.projects;
import java.util.Arrays;
import java.util.Comparator;
public class MaxCarsCh6App {
public static void main(String[] args) {
int[][] arr = {{1012, 1136}, {1317, 1417}, {1015, 1020}};
int[][] transformed;
transformed = transformArray(arr);
sortByTime(transformed);
for (int[] row : transformed) {
System.out.print(row[0] + " ");
System.out.println(row[1]);
}
System.out.println("Max Arrivals: " + getMaxOnes(transformed));
}
public static int[][] transformArray(int[][] arr) {
int[][] transformed = new int[arr.length*2][2];
for (int i = 0; i < arr.length; i++) {
transformed[i*2][0] = arr[i][0];
transformed[i*2][1] = 1;
transformed[i*2+1][0] = arr[i][1];
transformed[i*2+1][1] = 0;
}
return transformed;
}
public static void sortByTime(int[][] arr) {
Arrays.sort(arr, Comparator.comparing((int[] a) -> a[0]));
}
public static int getMaxOnes(int[][] arr) {
int times = 0;
int maxTimes = 0;
int i = 0;
while (i < arr.length) {
times = 0;
while ((i < arr.length) && (arr[i++][1] == 1)) {
times++;
}
if (times > maxTimes) maxTimes = times;
}
return maxTimes;
}
}