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.
This approach involves iterating over each year in the given range and counting how many people are alive each year. We then find and return the year with the maximum population count. Although this method may seem straightforward, it is inefficient for larger inputs due to the nested iteration over years.
This solution initializes an array to store population counts from 1950 to 2050. It then iterates over each person's birth-death range and increments the population for each year they are alive. Finally, it checks for the maximum population and returns the corresponding year.
Time Complexity: O(n * m), where n is the number of logs and m is the range of years (1950 to 2050).
Space Complexity: O(m), where m is the range of years (1950 to 2050).
This optimized method utilizes the concept of Delta Population. Instead of counting each year separately, we increase the population by one on a birth year and decrease it by one on the death year. We then compute the running sum of these changes across the year range to find peaks in population.
This solution utilizes an array to track changes in population for each year. By adding 1 to the birth year and subtracting 1 from the death year, we encode population changes directly. A running sum identifies the period of maximum population.
Time Complexity: O(n + m), n being the number of logs, m is fixed at 101 due to year constraints.
Space Complexity: O(m), utilizing a fixed array size for population deltas.
We notice that the range of years is [1950,..2050]. Therefore, we can map these years to an array d of length 101, where the index of the array represents the value of the year minus 1950.
Next, we traverse logs. For each person, we increment d[birth_i - 1950] by 1 and decrement d[death_i - 1950] by 1. Finally, we traverse the array d, find the maximum value of the prefix sum, which is the year with the most population, and add 1950 to get the answer.
The time complexity is O(n), and the space complexity is O(C). Where n is the length of the array logs, and C is the range size of the years, i.e., 2050 - 1950 + 1 = 101.
Python
Java
C++
Go
TypeScript
JavaScript
| Approach | Complexity |
|---|---|
| Brute Force Year Population Counting | Time Complexity: O(n * m), where n is the number of logs and m is the range of years (1950 to 2050). |
| Delta Population Technique | Time Complexity: O(n + m), n being the number of logs, m is fixed at 101 due to year constraints. |
| Difference Array | — |
| 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 |
1854 Maximum Population Year | Zero to FAANG Kunal | Assignment Solution | Leetcode • Programmers Zone • 8,480 views views
Watch 9 more video solutions →Practice Maximum Population Year with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor