0% completed
You are given a 2D integer array logs
containing the birth and death years of n
people. In logs
array, logs[i] = [birth<sub>i</sub>, death<sub>i</sub>] indicates the birth and death years of the i<sup>th</sup> person.
The population of a year is the number of people alive during that year. The i<sup>th</sup> person is counted in year x's population if x is in the inclusive range [birth<sub>i</sub>, death<sub>i</sub> - 1].
Return the earliest year with the highest population.
Example 1:
1994
3
people were alive (born in 1985, 1990, and 1994).Example 2:
1980
Example 3:
2005
Constraints:
To solve this problem, we can use a counting approach to keep track of population changes over the years. This approach involves using a single array to count the number of people born and another to count the number of people who have died. By iterating through these arrays, we can compute the population for each year and determine the year with the highest population.
This approach is efficient because it avoids the need to sort the years, making it faster. Instead, we directly compute the population changes and keep track of the maximum population year. This counting method ensures that we process each birth and death year once, leading to an optimal solution.
Initialize the Population Array:
population
of size 101 to store population changes for each year from 1950 to 2050.Record Births and Deaths:
log
in logs
:
population[log[0] - 1950]++
.population[log[1] - 1950]--
.Initialize Variables for Tracking Maximum Population:
maxPop
to 0: This will keep track of the maximum population encountered.maxYear
to 1950: This will store the year with the maximum population.currentPop
to 0: This will track the current population while iterating through the years.Calculate Maximum Population Year:
year
from 0 to 100:
currentPop
: currentPop += population[year]
. It adds positive or negative value to the currentPop
based on the number of births or deaths in the year
.currentPop
is greater than maxPop
:
maxPop
to currentPop
.maxYear
to the current year: maxYear = 1950 + year
.Return the Result:
maxYear
, which is the year with the maximum population.Using the input: [[1980, 1990], [1975, 1985], [1985, 1995], [1990, 2000], [1999, 2020], [1994, 2032]]
Initialization:
population
array of size 101 is initialized to all zeros.Processing logs:
[1980, 1990]
:
population[30]
by 1. (population[1980 - 1950]
)population[40]
by 1. (population[1990 - 1950]
)[1975, 1985]
:
population[25]
by 1. (population[1975 - 1950]
)population[35]
by 1. (population[1985 - 1950]
)[1985, 1995]
:
population[35]
by 1. (population[1985 - 1950]
)population[45]
by 1. (population[1995 - 1950]
)[1990, 2000]
:
population[40]
by 1. (population[1990 - 1950]
)population[50]
by 1. (population[2000 - 1950]
)[1999, 2020]
:
population[49]
by 1. (population[1999 - 1950]
)population[70]
by 1. (population[2020 - 1950]
)[1994, 2032]
:
population[44]
by 1. (population[1994 - 1950]
)population[82]
by 1. (population[2032 - 1950]
)Final population
array values at key years:
[0: 0, 25: 1, 30: 1, 35: 0, 40: 0, 44: 1, 45: -1, 49: 1, 50: -1, 70: -1, 82: -1]
Finding the maximum population year:
currentPop
= 0currentPop
+= population[25]
(1) -> currentPop
= 1maxPop
= 1, maxYear
= 1975currentPop
+= population[30]
(1) -> currentPop
= 2maxPop
= 2, maxYear
= 1980currentPop
+= population[35]
(0) -> currentPop
= 2maxPop
= 2, maxYear
= 1980currentPop
+= population[40]
(0) -> currentPop
= 2currentPop
+= population[44]
(1) -> currentPop
= 3maxPop
= 3, maxYear
= 1994currentPop
+= population[45]
(-1) -> currentPop
= 2maxPop
= 3, maxYear
= 1994currentPop
+= population[49]
(1) -> currentPop
= 3currentPop
+= population[50]
(-1) -> currentPop
= 2currentPop
+= population[70]
(-1) -> currentPop
= 1currentPop
+= population[82]
(-1) -> currentPop
= 0Final result:
The time complexity of the algorithm is O(N), where (N) is the number of entries in the logs. This is because we iterate through the logs to populate the population changes and then iterate through the years (which is a constant number of iterations, 101 to find the maximum population.
The space complexity is O(1) (constant space) because we use a fixed-size array of 101 elements to store the population changes for each year. The size of this array does not change with the input size.