📜  找到给定火车到达的平台

📅  最后修改于: 2021-10-28 01:58:24             🧑  作者: Mango

给定一个二维数组arr[][3]N 个列车的信息组成,其中arr[i][0]是列车号, arr[i][1]是到达时间, arr[i][2]是补时的持续时间。给定另一个代表列车号的整数F ,任务是根据以下规则找到列车号为F的列车到达的站台号:

  • 平台编号从1开始,平台数量是无限的。
  • 较早释放的站台分配给下一列火车。
  • 如果同时释放两个或更多平台,则列车到达具有最低平台编号的平台。
  • 如果有两列或更多列火车同时到达,则优先分配编号较小的火车。

例子:

方法:给定的问题可以通过使用优先级队列来解决。请按照以下步骤解决此问题:

  • 根据列车到达时间对给定的N列列车数组arr[]进行排序。
  • 初始化一个优先级队列,比如PQ{PlatformNumber, time} ,它根据最少的出发时间实现最小堆。在优先级队列中插入{平台号,时间},{1, 0}
  • 初始化一个 HashMap,比如存储任何火车到达的站台号码的M。
  • 遍历给定的数组arr[]并执行以下步骤:
    • 弹出PQ的顶级平台并将它们存储在free_platform[] 中
    • 如果当前列车的到站时间至少是站台的出发时间,则将站台的出发时间更新为(到站时间和停站时间之和+1)并插入站台当前状态在PQ和 HashMap M 中当前列车的当前站台号。
    • 否则,将新的平台条目添加到 HashMap M 中PQ和当前列车的当前平台号。
  • 完成上述步骤后,在 HashMap M 中打印与列车号F关联的站台号。

下面是上述方法的实现:

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 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 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 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 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));
    }
}


输出:
3

时间复杂度: O(N * log N)
辅助空间: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程