๐ฅ Algorithm/Baekjoon
[๋ฐฑ์ค 2579๋ฒ] ๊ณ๋จ์ค๋ฅด๊ธฐ - ์๋ฐ์คํฌ๋ฆฝํธ(nodejs)
Lennon
2022. 3. 11. 16:35
728x90
๋ฐ์ํ
https://www.acmicpc.net/problem/2579
const fs = require('fs');
let [n, ...input] = fs.readFileSync('dev/stdin').toString().trim().split('\n');
input = input.map(Number);
const dp = [];
dp[0] = input[0];
dp[1] = Math.max(input[0] + input[1], input[1]);
dp[2] = Math.max(input[0] + input[2], input[1] + input[2]);
for (let i = 3; i < input.length; i++) {
dp[i] = Math.max(input[i] + input[i - 1] + dp[i - 3], input[i] + dp[i - 2]);
}
console.log(dp[+n - 1]);
์ ํ์ ์ธ DP๋ฌธ์ ์ด๋ค. ์ ํ์์ ์๊ฐํ๋ ๊ฒ ์ค์ํ๋ค.
728x90
๋ฐ์ํ