Number of Airplanes in the Sky

mac2022-06-30  28

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; } };

转载于:https://www.cnblogs.com/deofly/p/number-of-airplanes-in-the-sky.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)