lintcdoe: Number of Airplanes in the Sky
2016-02-27 23:24
465 查看
Number of Airplanes in the Sky
30:00Given an interval list which are flying and landing time of the flight. How many airplanes are on the sky at most?
Have you met this question in a real interview?
Notice
If landing and flying happens at the same time, we consider landing should happen at first.
Example
Tags
Related Problems
Notes
For interval list
[[1,10],[2,3],[5,8],[4,7]], return
3
/** * Definition of Interval: * classs Interval { * int start, end; * Interval(int start, int end) { * this->start = start; * this->end = end; * } */ class Solution { public: /** * @param intervals: An interval array * @return: Count of airplanes are in the sky. */ int countOfAirplanes(vector<Interval> &airplanes) { // write your code here vector<int> departure; vector<int> landing; for (int i=0; i< airplanes.size(); i++) { departure.push_back(airplanes[i].start); landing.push_back(airplanes[i].end); } sort(departure.begin(), departure.end()); sort(landing.begin(), landing.end()); int airplane_in_sky = 1; int most_airplane_in_sky = 1; int i=1; int j=0; //因为首先要有一只飞机在天上飞,所以i要先于j while (i < departure.size() && j < landing.size()) { if (departure[i] < landing[j]) { airplane_in_sky++; if (airplane_in_sky > most_airplane_in_sky) { most_airplane_in_sky = airplane_in_sky; } i++; } else { airplane_in_sky--; j++; } } return most_airplane_in_sky; } };
相关文章推荐
- Leetcode ☞ 217. Contains Duplicate
- 【UVa】11413 – Fill the Containers
- GCJ--Millionaire (2008 APAC local onsites C)
- 关于main主函数的两个参数argc和argv
- ZOJ 3781 Paint the Grid Reloaded 图论 bfs
- 11.2 RAC: In "crsctl stat res -t" State Details May Be Missing or Incorrect (文档 ID 1086563.1)
- Hanoi Tower Troubles Again! (找规律)
- GetPostBackEventReference加RaisePostBackEvent实现自定义控件中回调传参
- Codeforces - AIM Tech Round (Div. 2)C - Graph and String
- ZOJ 3780 Paint the Grid Again
- 使用Keychain配置
- Leet Code OJ 70. Climbing Stairs [Difficulty: Easy]
- 1021 Fibonacci Again
- 集训队专题(7)1001 Drainage Ditches
- train validation test
- 1090 Higest Price in Supply Chain
- 102.Daikon Forge基础使用label、button、sprite、textureAtlas、font
- UVA 10976(p183)----Fractions Again?!
- VS2012 error C2664: “std::make_pair”:无法将左值绑定到右值引用
- 101.Daikon Forge插件介绍和安装