😎풀이

  1. logs를 출생일 기준 오름차 순 정렬
  2. 첫 출생자 년도부터 마지막 사망자 년도까지 순회
    2-1. 각 년도에 인구 수 조사
    2-2. 최대 인구보다 많은 해가 있다면 갱신
  3. 최대 인구수인 년도 반환
function maximumPopulation(logs: number[][]): number {
    const sorted = logs.toSorted((a, b) => a[0] - b[0])
    const firstBirth = sorted[0][0]
    const lastDeath = sorted.at(-1)[1]
    let maxPopYear = firstBirth
    let maxPopulation = 0
    for(let year = firstBirth; year < lastDeath; year++) {
        let curPopulation = 0
        for(const [targetB, targetD] of sorted) {
            if(targetB > year) continue
            if(targetD <= year) continue
            curPopulation++
        }
        if(maxPopulation >= curPopulation) continue
        maxPopYear = year
        maxPopulation = curPopulation
    }
    return maxPopYear
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글