天空の飛行機の数

説明

リストを考えると  interval離陸し、飛行時間を着陸されています、。空には、同時に最大でどのように多くの飛行機がありますか?

着陸と異なる面の離陸が同時に発生した場合、我々は着陸が最初に起こるべきことを検討してください。

例1:

Input: [(1, 10), (2, 3), (5, 8), (4, 7)]
Output: 3
Explanation:
The first airplane takes off at 1 and lands at 10.
The second ariplane takes off at 2 and lands at 3.
The third ariplane takes off at 5 and lands at 8.
The forth ariplane takes off at 4 and lands at 7.
During 5 to 6, there are three airplanes in the sky.

例2:

Input: [(1, 2), (2, 3), (3, 4)]
Output: 1
Explanation: Landing happen before taking off.

アイデア:走査線
/ ** 
 *間隔の定義:
 *公共classs間隔{ 
 * int型開始、終了。
 *間隔(int型開始、int型エンド){ 
 * this.start =始めます。
 * this.end =終了。
 *} 
 *} 
 * / 
クラスのイベント{ 
    int型の時間; 
    int型のフラグ。
     
    イベント(INT T、INT S){ 
        this.time = T。
        this.flag = S; 
    } 
} 
publicクラスソリューション{ 
    / ** 
     * @param飛行機:インターバル配列
     * @return:飛行機の数は空です。
     * / 
     パブリック静的コンパレータ<イベント> COMP =新しいコンパレータ<イベント>(){ 
        公共int型(イベントE1、イベントE2)を比較{
            IF(e1.time == e2.time){ 
                戻りe1.flag - e2.flag。
            } 
            戻りe1.time - e2.time。
        } 
    }。
    公共int型countOfAirplanes(一覧<間隔>飛行機){ 
         一覧<イベント>リスト=新しいArrayListを<>(); 
        (間隔I:飛行機)のために{ 
            list.add(新しいイベント(i.start、1))。
            list.add(新しいイベント(i.end、0)); 
        } 
         
        Collections.sort(リスト、COMP)。
        int型のカウント= 0、ANS = 0; 
        用(イベントe:リスト){ 
            場合(e.flag == 1){ 
                数- 。
            } 
                数++;
            }他{
            ANS = Math.max(ANS、カウント数)。
        } 
        ANSを返します。
    } 
}

  

おすすめ

転載: www.cnblogs.com/FLAGyuri/p/12077337.html