classSolution { public: intmaximumPopulation(vector<vector<int>>& logs){ vector<int> v(105); for (auto& log : logs) { for (int i = log[0]; i < log[1]; i++) { v[i - 1950]++; } } returnmax_element(v.begin(), v.end()) - v.begin() + 1950; } };
不需额外空间:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution { public: intmaximumPopulation(vector<vector<int>>& logs){ int ans = 0, cnt = 0; for (int i = 1950; i <= 2050; i++) { int t = 0; for (auto& log : logs) { if (i >= log[0] && i < log[1]) { t++; } } if (t > cnt) { cnt = t; ans = i; } } return ans; } };