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 };
  • 相关阅读:
    eclipse中切换jdk版本
    201704 创建财务凭证函数
    201704 F-47创建预付款申请a
    201704 F-02创建财务凭证
    20170413 F110学习
    20170411 F110初始界面-建议清单
    20170411 供应商保证金维护视图
    20170411 F-02创建财务凭证
    ABAP 断点篇-001
    20170411 debug窗口执行文件
  • 原文地址:https://www.cnblogs.com/easonliu/p/4504647.html
Copyright © 2011-2022 走看看