Problem Statement
Pattern:
Solution
public int maximumPopulation (int[][] logs){
final int MIN_YEAR = 1950, MAX_YEAR = 2050;
int[] population = new int[MAX_YEAR-MIN_YEAR+1];
// for each log
for(int[] log : logs){
// increment birth year population
population[log[0]-1950]++;
// decrement death year population
population[log[1]-1950]--;
}
// convert to prefix sum to get true population
for (int i = 1; i < population.length; i++)
population[i] += population[i-1];
// find maxYear
int maxYear = 0;
for (int i = 0; i < population.length; i++)
if(population[i] > population[maxYear]) maxYear = i;
// offset to get answer
return maxYear+1950;
}