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

📅  最后修改于: 2021-05-04 20:34:38             🧑  作者: Mango

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

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

例子:

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

  • 根据火车的到达时间对N列火车的给定数组arr []进行排序。
  • 初始化一个优先级队列,说成对{PlatformNumber,time}的PQ ,该队列根据最小的离开时间实现最小堆。在优先级队列中插入{平台编号,时间},{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;
    }
}
  
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);
  
        // 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(
            "112563", 5, 5));
        trains.add(new Train(
            "112569", 7, 3));
        trains.add(new Train(
            "112560", 3, 7));
        String F = "112569";
  
        System.out.println(
            findPlatformOf(
                trains, trains.size(), F));
    }
}


输出:
3


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