Number of Airplanes in the Sky

Description:

Given an array of integers and a number k, the majority number is the number that occurs more than 1/k of the size of the array.

Find it.

Example

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

Solution:

/**
 * 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) {
		vector<pair<int, int>> vec;
		for (auto& e : airplanes) {
			vec.push_back(make_pair(e.start, 1));
			vec.push_back(make_pair(e.end, -1));
		}
		sort(begin(vec), end(vec));
		int rc = 0, cnt = 0;
		for (auto& e : vec) {
			cnt += e.second;
			rc = max(rc, cnt);
		}
		return rc;
	}
};
posted @ 2015-09-15 15:30  影湛  阅读(122)  评论(0编辑  收藏  举报