描述:
给出一个无重叠的按照区间起始端点排序的区间列表。
在列表中插入一个新的区间,你要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间)。
样例
样例 1:
输入:
(2, 5) into [(1,2), (5,9)]
输出:
[(1,9)]
样例 2:
输入:
(3, 4) into [(1,2), (5,9)]
输出:
[(1,2), (3,4), (5,9)]、
代码:
/**
* Definition of Interval:
* classs Interval {
* int start, end;
* Interval(int start, int end) {
* this->start = start;
* this->end = end;
* }
* }
*/
class Solution {
public:
/**
* @param airplanes: An interval array
* @return: Count of airplanes are in the sky.
*/
int countOfAirplanes(vector<Interval> &airplanes) {
// write your code here
int n = airplanes.size();
int flag[1000];
for(int i=0; i<1000; ++i)flag[i]=0;
for(int i=0; i<n; ++i){
for(int j=airplanes[i].start; j<=airplanes[j].end; ++j){
flag[j]=flag[j]+1;
}
}
int max = -1;
for(int i=0; i<1000; ++i){
if(flag[i]>max) max = flag[i];
}
return max;
}
};
好菜啊。。。