leetcode 面试题 16.10. 生存人数

class Solution:
    def maxAliveYear(self, birth: List[int], death: List[int]) -> int:
        # 统计每一年出生与死亡的人数
        b = Counter(birth)
        d = Counter(death)
        # 初始化活着的人数n、最大存活数maxn、最大存活数对应年份
        n, maxn, year = 0, 0, 0
        # 迭代所有年份
        for i in range(1900, 2001):
            # 存活的人数 = n+今年出生人数-去年死亡人数
            n += b[i]
            n -= d[i-1]
            if n > maxn: 
                maxn, year= n, i
        return year

猜你喜欢

转载自blog.csdn.net/milk_paramecium/article/details/122568607