Similar Problems

Similar Problems not available

Maximum Population Year - Leetcode Solution

Companies:

LeetCode:  Maximum Population Year Leetcode Solution

Difficulty: Easy

Topics: prefix-sum array  

The Maximum Population Year problem on LeetCode is:

You are given a list of people with their birth and death years. Find the year with the highest population. A person's lifespan is defined as the number of years between their birth and death years. The year with the highest population is the one in which the maximum number of people were alive.

This can be solved using a simple algorithm:

  1. Create a dictionary to store the count of people alive for each year between the minimum birth year and maximum death year.

  2. Loop through the list of people and for each person, increment the count in the dictionary for each year they were alive.

  3. Find the year with the highest population count in the dictionary.

  4. Return the year with the highest population count.

Here is the python code that implements this algorithm:

def maxPopulationYear(people): # Create a dictionary to store the count of people alive for each year population = {} for person in people: # Increment the count for each year the person was alive for year in range(person[0], person[1]): if year in population: population[year] += 1 else: population[year] = 1

# Find the year with the highest population count
max_population = 0
max_year = 0
for year, count in population.items():
    if count > max_population:
        max_population = count
        max_year = year

# Return the year with the highest population count
return max_year

The time complexity of this algorithm is O(N*M) where N is the number of people and M is the range between the minimum birth year and maximum death year. However, if we assume that M is a constant value, then the time complexity becomes O(N).

Overall, this is a simple and efficient solution to the Maximum Population Year problem on LeetCode.

Maximum Population Year Solution Code

1