5. Maximum Population Year
Input: logs = [[1993,1999],[2000,2010]]
Output: 1993
Explanation: The maximum population is 1, and 1993 is the earliest year with this population.Input: logs = [[1950,1961],[1960,1971],[1970,1981]]
Output: 1960
Explanation:
The maximum population is 2, and it had happened in years 1960 and 1970.
The earlier year between them is 1960.Solution: (Similar to car pooling)
class Solution
{
public:
int maximumPopulation(vector<vector<int>> &logs)
{
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
for (int i = 0; i < logs.size(); i++)
{
pq.push({logs[i][0], 1});
pq.push({logs[i][1], -1});
}
int maxPeople = 0;
int maxYear = 0;
int person = 0;
while (!pq.empty())
{
int year = pq.top().first;
int c = pq.top().second;
pq.pop();
person += c;
if (person > maxPeople)
{
maxPeople = person;
maxYear = year;
}
}
return maxYear;
}
};Last updated