Given a 2D array **arr[][3]** consisting of information of **N** trains where **arr[i][0]** is the train number, **arr[i][1]** is the arrival time, and **arr[i][2]** is the duration of stoppage time. Given another integer **F** representing the train number, the task is to find the platform number on which the train with train number **F** arrives according to the following rules:

- Platform numbering starts from
**1**and there is an infinite number of platforms. - The platform which is freed earlier is allocated to the next train.
- If two or more platforms are freed at the same time then the train arrives at the platform with the lowest platform number.
- If two or more trains arriving at the same time, then the train with a smaller train number is allocated first.

**Examples:**

Input:arr[] = {{112567, 1, 2}, {112563, 3, 3}, {112569, 4, 7}, {112560, 9, 3}}, F = 112569Output:1Explanation:

Below is the order of the arrival of trains:

Train Platform Leaving Time

112567 1 4

112563 2 71125691 12

112560 2 13Therefore, the train with train number 112569 arrives at platform number 1.

Input:arr[] = {{112567, 2, 1}, {112563, 5, 5}, {112569, 7, 3}, {112560, 3, 7}}, F = 112569Output:3

**Approach:** The given problem can be solved by using the priority queue. Follow the steps below to solve this problem:

- Sort the given array
**arr[]**of**N**trains according to the arrival time of the trains. - Initialize a priority queue, say
**PQ**of pairs**{PlatformNumber, time}**that implements the min-heap according to the least departure time. Insert the**{platform number, time}**i.e.,**{1, 0}**in the priority queue. - Initialize a HashMap, say
**M**that stores the platform number on which any train arrives. - Traverse the given array
**arr[]**and perform the following steps:- Pop the top platform of the
**PQ**and store them in**free_platform[]**. - If the arrival time of the current train is at least the departure time of the popped platform, then update the departure time of the popped platform as the
**(sum of the arrival and the stoppage time + 1)**and insert the current status of the platform in**PQ**and the current platform number of the current train in the HashMap**M**. - Otherwise, add the new platform entry to the
**PQ**and the current platform number of the current train in the HashMap**M**.

- Pop the top platform of the
- After completing the above steps, print the platform number associated with the train number
**F**in the HashMap**M**.

Below is the implementation of the above approach:

## Java

`// Java program for the above approach` `import` `java.util.*;` `// Stores the information for each` `// train as Objects` `class` `Train {` ` ` `// Stores the train number` ` ` `String train_num;` ` ` `// Stores the arrival time` ` ` `int` `arrival_time;` ` ` `// Stores the stoppage time` ` ` `int` `stoppage_time;` ` ` `// Constructor` ` ` `Train(String train_num,` ` ` `int` `arrival_time,` ` ` `int` `stoppage_time)` ` ` `{` ` ` `this` `.train_num = train_num;` ` ` `this` `.arrival_time = arrival_time;` ` ` `this` `.stoppage_time = stoppage_time;` ` ` `}` `}` `public` `class` `GFG {` ` ` `// Function to find the platform` ` ` `// on which train F arrives` ` ` `static` `int` `findPlatformOf(` ` ` `ArrayList<Train> trains, ` `int` `n,` ` ` `String F)` ` ` `{` ` ` `// Sort the array arr[] according` ` ` `// to the arrival time` ` ` `Collections.sort(` ` ` `trains,` ` ` `(a, b) -> a.arrival_time==b.arrival_time ? Integer.parseInt(a.train_num)-Integer.parseInt(b.train_num) : a.arrival_time - b.arrival_time);` ` ` `// Stores the platforms that` ` ` `// is in currently in use` ` ` `PriorityQueue<` `int` `[]> pq` ` ` `= ` `new` `PriorityQueue<>(` ` ` `(a, b)` ` ` `-> a[` `1` `] == b[` `1` `] ? a[` `0` `] - b[` `0` `]` ` ` `: a[` `1` `] - b[` `1` `]);` ` ` `// Insert the platform number 1` ` ` `// with departure time as 0` ` ` `pq.add(` `new` `int` `[] { ` `1` `, ` `0` `});` ` ` `// Store the platform number` ` ` `// on which train arrived` ` ` `HashMap<String, Integer> schedule` ` ` `= ` `new` `HashMap<>();` ` ` `// Traverse the given array` ` ` `for` `(Train t : trains) {` ` ` `// Pop the top platform of` ` ` `// the priority queue` ` ` `int` `[] free_platform = pq.poll();` ` ` `// If arrival time of the train` ` ` `// >= freeing time of the platform` ` ` `if` `(t.arrival_time >= free_platform[` `1` `]) {` ` ` `// Update the train status` ` ` `free_platform[` `1` `]` ` ` `= t.arrival_time + t.stoppage_time + ` `1` `;` ` ` `// Add the current platform` ` ` `// to the pq` ` ` `pq.add(free_platform);` ` ` `// Add the platform` ` ` `// number to the HashMap` ` ` `schedule.put(t.train_num,` ` ` `free_platform[` `0` `]);` ` ` `}` ` ` `// Otherwise, add a new platform` ` ` `// for the current train` ` ` `else` `{` ` ` `// Update the priority queue` ` ` `pq.add(free_platform);` ` ` `// Get the platform number` ` ` `int` `platform_num = pq.size() + ` `1` `;` ` ` `// Add the platform to` ` ` `// the priority queue` ` ` `pq.add(` `new` `int` `[] {` ` ` `platform_num,` ` ` `t.arrival_time` ` ` `+ t.stoppage_time + ` `1` `});` ` ` `// Add the platform` ` ` `// number to the HashMap` ` ` `schedule.put(t.train_num,` ` ` `platform_num);` ` ` `}` ` ` `}` ` ` `// Return the platform on` ` ` `// which F train arrived` ` ` `return` `schedule.get(F);` ` ` `}` ` ` `// Driver Code` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` `ArrayList<Train> trains` ` ` `= ` `new` `ArrayList<>();` ` ` `trains.add(` `new` `Train(` ` ` `"112567"` `, ` `2` `, ` `1` `));` ` ` `trains.add(` `new` `Train(` ` ` `"112569"` `, ` `5` `, ` `5` `));` ` ` `trains.add(` `new` `Train(` ` ` `"112563"` `, ` `5` `, ` `3` `));` ` ` `trains.add(` `new` `Train(` ` ` `"112560"` `, ` `3` `, ` `7` `));` ` ` `String F = ` `"112563"` `;` ` ` `System.out.println(` ` ` `findPlatformOf(` ` ` `trains, trains.size(), F));` ` ` `}` `}` |

**Output:**

3

**Time Complexity:** O(N * log N) **Auxiliary Space:** O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend live classes with industry experts, please refer **Geeks Classes Live**