-
Notifications
You must be signed in to change notification settings - Fork 688
/
MinimumPlatforms.java
69 lines (52 loc) · 1.69 KB
/
MinimumPlatforms.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
69
package math;
import java.util.Arrays;
public class MinimumPlatforms {
/*
* Given arrival and departure times of all trains that reach a railway station,
* find the minimum number of platforms required for the railway station so that no train waits.
*
* Input: arr[] = {9:00, 9:40, 9:50, 11:00, 15:00, 18:00}
* dep[] = {9:10, 12:00, 11:20, 11:30, 19:00, 20:00}
* Output: 3
*
*
* */
static int findPlatform(int arr[], int dep[], int n) {
// Sort arrival and departure arrays
Arrays.sort(arr);
Arrays.sort(dep);
/* plat_needed indicates number of platforms
* needed at a time
*/
int plat_needed = 1, result = 1;
int i = 1, j = 0;
/* Similar to merge in merge sort to process
* all events in sorted order
*/
while (i < n && j < n) {
/* If next event in sorted order is arrival,
* increment count of platforms needed
* */
if (arr[i] <= dep[j]) {
plat_needed++;
i++;
if (plat_needed > result)
result = plat_needed;
}
/*
* Else decrement count of platforms needed
* */
else {
plat_needed--;
j++;
}
}
return result;
}
public static void main(String[] args) {
int arr[] = {900, 940, 950, 1100, 1500, 1800};
int dep[] = {910, 1200, 1120, 1130, 1900, 2000};
int n = arr.length;
System.out.println("Minimum Number of Platforms Required = " + findPlatform(arr, dep, n));
}
}