Population Alive in a Target Year
Problem statement
The parallel arrays birthYears and deathYears describe a population. Person i is alive in every integer year from birthYears[i] through deathYears[i], inclusive.
Given an integer year, return how many people are alive during that year. Return 0 when the population is empty.
Function
countPopulationInYear(birthYears: int[], deathYears: int[], year: int) → intExamples
Example 1
birthYears = [1900,1920,1930]deathYears = [1950,1940,1970]year = 1935return = 3All three people were born by 1935 and none had died before 1935.
Example 2
birthYears = [2000,1990,2010]deathYears = [2000,1999,2015]year = 2000return = 1The first person counts in both the birth and death year. The second died before 2000, and the third was born later.
Example 3
birthYears = []deathYears = []year = 2025return = 0An empty population has nobody alive in any year.
Constraints
0 <= birthYears.length = deathYears.length <= 100000.0 <= birthYears[i] <= deathYears[i] <= 1000000000.0 <= year <= 1000000000.