1854. Maximum Population Year LeetCode Solution

In this guide, you will get 1854. Maximum Population Year LeetCode Solution with the best time and space complexity. The solution to Maximum Population Year problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Maximum Population Year solution in C++
  4. Maximum Population Year solution in Java
  5. Maximum Population Year solution in Python
  6. Additional Resources
1854. Maximum Population Year LeetCode Solution image

Problem Statement of Maximum Population Year

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 <= 100
1950 <= birthi < deathi <= 2050

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

1854. Maximum Population Year LeetCode Solution in C++

class Solution {
 public:
  int maximumPopulation(vector<vector<int>>& logs) {
    constexpr int kMinYear = 1950;
    constexpr int kMaxYear = 2050;
    int ans = 0;
    int maxPopulation = 0;
    int runningPopulation = 0;
    // population[i] := the population of year i
    vector<int> population(kMaxYear + 1);

    for (const vector<int>& log : logs) {
      const int birth = log[0];
      const int death = log[1];
      ++population[birth];
      --population[death];
    }

    for (int year = kMinYear; year <= kMaxYear; ++year) {
      runningPopulation += population[year];
      if (runningPopulation > maxPopulation) {
        maxPopulation = runningPopulation;
        ans = year;
      }
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

1854. Maximum Population Year LeetCode Solution in Java

class Solution {
  public int maximumPopulation(int[][] logs) {
    final int kMinYear = 1950;
    final int kMaxYear = 2050;
    int ans = 0;
    int maxPopulation = 0;
    int runningPopulation = 0;
    // population[i] := the population of year i
    int[] population = new int[kMaxYear + 1];

    for (int[] log : logs) {
      final int birth = log[0];
      final int death = log[1];
      ++population[birth];
      --population[death];
    }

    for (int year = kMinYear; year <= kMaxYear; ++year) {
      runningPopulation += population[year];
      if (runningPopulation > maxPopulation) {
        maxPopulation = runningPopulation;
        ans = year;
      }
    }

    return ans;
  }
}
// code provided by PROGIEZ

1854. Maximum Population Year LeetCode Solution in Python

class Solution:
  def maximumPopulation(self, logs: list[list[int]]) -> int:
    kMinYear = 1950
    kMaxYear = 2050
    ans = 0
    maxPopulation = 0
    runningPopulation = 0
    # population[i] := the population of year i
    population = [0] * (kMaxYear + 1)

    for birth, death in logs:
      population[birth] += 1
      population[death] -= 1

    for year in range(kMinYear, kMaxYear + 1):
      runningPopulation += population[year]
      if runningPopulation > maxPopulation:
        maxPopulation = runningPopulation
        ans = year

    return ans
# code by PROGIEZ

Additional Resources

See also  829. Consecutive Numbers Sum LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.