😎풀이

  1. nums의 수 * 빈도 형태로 points에 저장
  2. 모든 수를 순회하며 다음 조건 수행
    2-1. 현재 수를 모두 획득한다.
    2-2. 현재 수를 획득하지 않는다.
  3. 획득 가능한 최대 수익을 반환한다.
function deleteAndEarn(nums: number[]): number {
    const maxNum = Math.max(...nums)
    const points = Array(maxNum + 1).fill(0)
    for(const num of nums) {
        points[num] += num
    }
    let take = 0
    let skip = 0
    for(let i = 0; i <= maxNum; i++) {
        const curTake = skip + points[i]
        const curSkip = Math.max(skip, take)
        take = curTake
        skip = curSkip
    }
    return Math.max(take, skip)
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글