[programmers/js] 파괴되지 않은 건물

승민·2025년 1월 13일

알고리즘

목록 보기
135/171

파괴되지 않은 건물

https://school.programmers.co.kr/learn/courses/30/lessons/92344

문제 설명

링크 참고

풀이

function solution(board, skill) {
    const nBoard = Array.from({length: board.length + 1}, () => Array.from({length: board[0].length + 1 }, _ => 0))
    
    for (const [type, r1, c1, r2, c2, degree] of skill) {
        const d = type === 1 ? -degree : degree
        nBoard[r1][c1] += d
        nBoard[r2 + 1][c1] -= d
        nBoard[r1][c2 + 1] -= d
        nBoard[r2 + 1][c2 + 1] += d
    }
    
    // 누적 합
    for (let i = 0; i < board.length; i++) {
        for (let j = 0; j < board[0].length; j++){
            nBoard[i + 1][j] += nBoard[i][j]
        }
    }
    
    for (let i = 0; i < board.length; i++) {
        for (let j = 0; j < board[0].length; j++){
            nBoard[i][j + 1] += nBoard[i][j]
        }
    }
    
    for (let i = 0; i < board.length; i++) {
        for (let j = 0; j < board[0].length; j++){
            board[i][j] += nBoard[i][j]
        }
    }
    
    return board.map((row) => row.filter((v) => v > 0).length).reduce((a, c) => a + c, 0)
}

0개의 댓글