Watch 10 video solutions for Maximum Population Year, a easy level problem involving Array, Counting, Prefix Sum. This walkthrough by Programmers Zone has 8,480 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
Example 1:
Input: logs = [[1993,1999],[2000,2010]] Output: 1993 Explanation: The maximum population is 1, and 1993 is the earliest year with this population.
Example 2:
Input: logs = [[1950,1961],[1960,1971],[1970,1981]] Output: 1960 Explanation: The maximum population is 2, and it had happened in years 1960 and 1970. The earlier year between them is 1960.
Constraints:
1 <= logs.length <= 1001950 <= birthi < deathi <= 2050Problem Overview: You are given birth and death years for multiple people. Each log [birth, death] means the person was alive from birth up to but not including death. The task is to determine the year with the highest population. If multiple years share the same population, return the earliest year.
Approach 1: Brute Force Year Population Counting (Time: O(n * R), Space: O(1))
This method directly simulates population counts for every year. For each year in the allowed range (1950–2050 in the problem constraints), iterate through all logs and count how many people are alive in that year. A person contributes to the count if birth ≤ year < death. Track the maximum population seen so far and update the answer when a larger count appears. The implementation is simple and relies only on basic iteration over an array of logs.
The key idea is straightforward counting: evaluate each year independently. While this works well given the small fixed year range, it becomes inefficient if the range grows large because every year scans all logs again. Time complexity is O(n * R) where n is the number of logs and R is the number of candidate years. Space complexity stays O(1) since only a few counters are maintained.
Approach 2: Delta Population Technique (Prefix Sum) (Time: O(n + R), Space: O(R))
The optimized solution treats population changes as events instead of recomputing counts every year. For each log, increment the population at birth and decrement it at death. This creates a difference array where each index represents how the population changes in that year. After recording all changes, run a cumulative sum across the years to reconstruct the population count at each point.
This is a classic application of the prefix sum pattern combined with simple counting. The insight is that births and deaths define boundaries where population changes, so tracking only those transitions avoids repeatedly scanning all logs. After computing the prefix sum, iterate once through the years to find the maximum population and return the earliest year where it occurs.
This approach runs in O(n + R) time: O(n) to record birth/death deltas and O(R) to compute the prefix sum across the year range. Space complexity is O(R) for the difference array. For problems involving frequent range updates and cumulative totals, the delta technique is a powerful pattern.
Recommended for interviews: Interviewers typically expect the delta population (prefix sum) approach. The brute force version demonstrates that you understand the population definition and boundary condition birth ≤ year < death. The prefix sum solution shows stronger algorithmic thinking by converting repeated counting into event-based updates and a single cumulative pass.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Year Population Counting | O(n * R) | O(1) | Good for understanding the problem or when the year range is very small |
| Delta Population Technique (Prefix Sum) | O(n + R) | O(R) | Best general solution when many intervals affect cumulative counts |