Maximum Population Year - Problem
You are given a 2D integer array logs where each logs[i] = [birthi, deathi] 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 [birthi, deathi - 1]. Note that the person is not counted in the year that they die.
Return the earliest year with the maximum population.
Input & Output
Example 1 — Basic Case
$
Input:
logs = [[1993,1999],[2000,2010]]
›
Output:
1993
💡 Note:
Person 1 lived from 1993-1998 (not counting 1999). Person 2 lived from 2000-2009. Each year has population 1, so return earliest year 1993.
Example 2 — Overlapping Lives
$
Input:
logs = [[1950,1961],[1960,1971],[1970,1981]]
›
Output:
1960
💡 Note:
Years 1960-1970 have 2 people alive (persons 1&2 overlap in 1960, persons 2&3 overlap in 1970). Return earliest year with max population: 1960.
Example 3 — Single Person
$
Input:
logs = [[2000,2050]]
›
Output:
2000
💡 Note:
Only one person alive from 2000-2049. Population is 1 for all these years, return earliest: 2000.
Constraints
- 1 ≤ logs.length ≤ 100
- 1950 ≤ birthi < deathi ≤ 2050
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array of [birth, death] pairs for each person
2
Process
Track population changes using events
3
Output
Earliest year with maximum population
Key Takeaway
🎯 Key Insight: Use difference arrays to efficiently track population changes at specific events rather than checking every year
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code