-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
72083ed
commit f92f5f1
Showing
2 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include <cstddef> | ||
#include <cstdlib> | ||
#include <map> | ||
#include <vector> | ||
|
||
class Solution { | ||
public: | ||
int maximumPopulation(std::vector<std::vector<int>>& logs) { | ||
// logs[i] = [birth, death], birth/death year of the ith person. | ||
// population is the number of people alive during that year. | ||
// i-th person is counted in year x population if x is in the inclusive | ||
// range [birth, death-1], | ||
// Want: earliest year with the maximum population. | ||
// Line Sweep. | ||
// Events are the birth/death years. If birth, add 1 to the prefix sum, | ||
// if death, -1. Take the max of the sum. | ||
|
||
// {year, count} | ||
std::map<int, int> events; | ||
for (const auto& log : logs) { | ||
int birth = log[0]; | ||
int death = log[1]; | ||
|
||
++events[birth]; | ||
--events[death]; | ||
} | ||
|
||
int maxPop = 0; | ||
int maxYear = 0; | ||
|
||
int pop = 0; | ||
for (const auto [year, count] : events) { | ||
pop += count; | ||
if (pop > maxPop) { | ||
maxPop = pop; | ||
maxYear = year; | ||
} | ||
} | ||
return maxYear; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters