kakasoo

[node.js] 스티커 ( 백준 9465번 ) 본문

프로그래밍/알고리즘 풀이

[node.js] 스티커 ( 백준 9465번 )

카카수(kakasoo) 2021. 3. 25. 18:40
반응형
const readline = require("readline");
const input = [];
let count = 0;

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
});

rl.on("line", (line) => {
    if (!count) {
        count = 3 * line;
    } else {
        input.push(line);

        if (input.length % 3 === 0) {
            main();
        }

        if (input.length === count) {
            rl.close();
        }
    }
}).on("close", () => {
    process.exit();
});

const main = () => {
    const post = input.slice(-2).map((el) => el.split(" ").map(Number));
    const DP = new Array(post[0].length).fill(0);
    for (let i = 0; i < post[0].length; i++) {
        DP[i] = [];
    }
    DP[0] = [post[0][0], post[1][0]];

    for (let i = 1; i < post[0].length; i++) {
        DP[i][0] = DP[i - 1][1] + post[0][i];
        DP[i][1] = DP[i - 1][0] + post[1][i];
        if (i >= 2) {
            DP[i][0] = Math.max(
                DP[i][0],
                DP[i - 2][0] + post[0][i],
                DP[i - 2][1] + post[0][i]
            );

            DP[i][1] = Math.max(
                DP[i][1],
                DP[i - 2][0] + post[1][i],
                DP[i - 2][1] + post[1][i]
            );
        }
    }
    console.log(Math.max(DP[post[0].length - 1][0], DP[post[0].length - 1][1]));
};

자바스크립트 특성 상 2차원 배열로 가면 정신이 해로워지는 거 같다.
문제 자체는 그냥 DP다.
DP(n)은 DP(n-1) 까지의 최대 합과 현재 더할 수 있는 우표의 합 또는,
DP(n-2)와 현재 더할 수 있는 우표의 합 2개,
총 3가지 중 max값이다.

반응형