跳转至

人口最多的年份⚓︎

Leetcode题目链接

描述⚓︎

详见中文题目链接

解答⚓︎

class Solution {
public:
    int maximumPopulation(vector<vector<int>>& logs) {
        vector<pair<int, char>> events;
        for (const auto& log : logs) {
            events.emplace_back(log[0], 's');
            events.emplace_back(log[1], 'e');
        }

        sort(events.begin(), events.end(), [](const auto& lhs, const auto& rhs) {
            return lhs.first < rhs.first || (lhs.first == rhs.first && lhs.second < rhs.second);
        });

        int currentOverlap = 0, maxOverlap = 0;
        int res = events[0].first;
        for (const auto& event : events) {
            if (event.second == 's') {
                currentOverlap++;
            } else {
                currentOverlap--;
            }
            if (currentOverlap > maxOverlap) {
                maxOverlap = currentOverlap;
                res = event.first;
            }
        }
        return res;
    }
};