ASMR LeetCode: Maximum Population Year | Chill Coding

Поделиться
HTML-код
  • Опубликовано: 26 ноя 2024

Комментарии • 1

  • @LeekCodeDev
    @LeekCodeDev  4 дня назад +1

    Source Code:
    class Solution:
    def maximumPopulation(self, logs: List[List[int]]) -> int:
    '''
    To solve this problem, I first thought of creating a basic array
    that has indices that represent the years of people that were
    still alive during that time, and increment 1 for each year for that
    person was alive. I keep doing this for each person, and at the end,
    this array will have one year where we had the most number of
    people alive. I will return this index as a year and get our answer!
    :D
    '''
    max_years = 0
    best_year = 0
    years_alive = [0] * 101
    for birth,death in logs:
    for i in range(death-birth):
    years_alive[birth-1950+i] += 1
    for i in range(len(years_alive)):
    if years_alive[i] > max_years:
    max_years = years_alive[i]
    best_year = i
    return best_year + 1950