Posts Minimum Platforms (geeksforgeeks - SDE Sheet)
Post
Cancel

Minimum Platforms (geeksforgeeks - SDE Sheet)

PROBLEM DESCRIPTION

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 is kept waiting. Consider that all the trains arrive on the same day and leave on the same day. Arrival and departure time can never be the same for a train but we can have arrival time of one train equal to departure time of the other. At any given instance of time, same platform can not be used for both departure of a train and arrival of another train. In such cases, we need different platforms.

geeksforgeeks

SOLUTION

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
class Solution
{
    //Function to find the minimum number of platforms required at the
    //railway station such that no train waits.
    static int findPlatform(int arr[], int dep[], int n)
    {

        if(n == 0)
            return 0;

        // sort arrival so that we can first check if any platform can be made available for the earliest required train
        Arrays.sort(arr);

        // we will sort the departure times also
        // init: the first endtime will be added to minheap for comparison with rest of the trains
        Arrays.sort(dep);

        PriorityQueue<Integer> pq = new PriorityQueue<>();
        pq.add(dep[0]); // first/earliest endTime of the train

        // at least one platform is needed for the first train
        int platforms = 1;

        // check other trains
        for(int i=1; i<n; i++){

            // current start the train
            int currentArr = arr[i];

            // If the earliest departing train's departure time is before the current train's arrival time,
            // that platform can be freed up. Remove that departure time from the priority queue.
            if(pq.peek() < currentArr){
                pq.poll();

            // If no platform is free, increase the platform count.
            }else{
                platforms++;
            }

            // add the departure time of current train
            pq.add(dep[i]);

        }

        return platforms;

    }

}
This post is licensed under CC BY 4.0 by the author.