DSA problem
Maximum Population Year
You are given a 2D integer array logs where each logs[i] = [birth_i, death_i] indicates the birth and death years of the ith person.
The population of some year x is the number of people alive during that year. The ith person is counted in year x's population if x is in the inclusive range [birth_i, death_i - 1]. Note that the person is not counted in the year that they die.
Return the earliest year with the maximum population.
This is a difference-array problem: instead of incrementing every year of a person's lifespan, mark +1 at the birth year and -1 at the death year, then take a running sum to recover the population per year.
Examples
logs = [[1993,1999],[2000,2010]]1993logs = [[1950,1961],[1960,1971],[1970,1981]]1960logs = [[2008,2026],[2004,2008],[2008,2026]]2008Constraints
- 1 <= logs.length <= 100
- 1950 <= birth_i < death_i <= 2050
3 hints and a full solution walkthrough with step-by-step visualizations are available in the practice editor.