zoukankan      html  css  js  c++  java
  • [LintCode] Number of Airplanes in the Sky

    Given an interval list which are flying and landing time of the flight. How many airplanes are on the sky at most?

    Example

    For interval list [[1,10],[2,3],[5,8],[4,7]], return 3

    Note

    If landing and flying happens at the same time, we consider landing should happen at first.

    http://www.lintcode.com/en/problem/number-of-airplanes-in-the-sky/

    将所有区间的start与end放在一起排序,但是要标记其是属性,然后统一排序,问题就转化成了括号匹配嵌套的问题了(最大有多少层括号嵌套,比如说((()))就是一个3层嵌套,()(()))最大嵌套是2),这里start相当于左括号,end相当于右括号,只要用一个cnt来记录,遇到start就加1,遇到end就减1,记录过程中的最大值就是答案。 

     1 /**
     2  * Definition of Interval:
     3  * classs Interval {
     4  *     int start, end;
     5  *     Interval(int start, int end) {
     6  *         this->start = start;
     7  *         this->end = end;
     8  *     }
     9  */
    10 class Solution {
    11 public:
    12     /**
    13      * @param intervals: An interval array
    14      * @return: Count of airplanes are in the sky.
    15      */
    16     int countOfAirplanes(vector<Interval> &airplanes) {
    17         // write your code here
    18         vector<pair<int, bool> > v;
    19         for (auto &i : airplanes) {
    20             v.push_back(make_pair(i.start, true));
    21             v.push_back(make_pair(i.end, false));
    22         }
    23         int cnt = 0, res = 0;
    24         sort(v.begin(), v.end());
    25         for (auto &i : v) {
    26             if (i.second) ++cnt;
    27             else --cnt;
    28             res = max(cnt, res);
    29         }
    30         return res;
    31     }
    32 };
  • 相关阅读:
    4.12 活跃性
    4.13 ReentrantLock
    异步模式之生产者消费者
    同步模式之保护性暂停Guarded Suspension
    android 学习
    android 学习
    android 学习
    android 学习
    android 学习
    android 学习(开课博客)
  • 原文地址:https://www.cnblogs.com/easonliu/p/4504647.html
Copyright © 2011-2022 走看看