-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountPlatform.java
More file actions
65 lines (56 loc) · 1.71 KB
/
CountPlatform.java
File metadata and controls
65 lines (56 loc) · 1.71 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
56
57
58
59
60
61
62
63
64
65
package algorithm.greedyalgorithm;
import java.util.Arrays;
/**
* The type Count platform.
*/
public class CountPlatform {
/**
* Count platform int.
*
* @param arrival the arrival
* @param departure the departure
* @return the int
*/
public static int countPlatform(int[] arrival, int[] departure) {
int n = arrival.length;
Arrays.sort(arrival);
Arrays.sort(departure);
int platform = 1;
int result = 1;
int i = 1;
int j = 0;
while ((i < n) & (j < n)) {
if (arrival[i] < departure[j]) {
platform++;
i++;
//Update result if needed
if (platform > result) {
result = platform;
}
} else {
//Else decrement count of platforms needed
platform--;
j += 1;
}
}
return result;
}
/**
* The entry point of application.
*
* @param args the input arguments
*/
public static void main(String[] args) {
//Example 1
int arrival[] = {900, 940, 950, 1100, 1500, 1800};
int departure[] = {910, 1200, 1120, 1130, 1900, 2000};
int answer = countPlatform(arrival, departure);
System.out.println("Minimum Number of Platforms Required for Plan1 = " + answer);
System.out.println();
// Example 2
int arrival1[] = {200, 210, 300, 320, 350, 500};
int departure1[] = {230, 240, 320, 430, 400, 520};
int answer2 = countPlatform(arrival1, departure1);
System.out.println("Minimum Number of Platforms Required for Plan2 = " + answer2);
}
}