Skip to content

Commit

Permalink
WIP: dp(낮잠 시간)
Browse files Browse the repository at this point in the history
  • Loading branch information
luckylooky2 committed Apr 25, 2024
1 parent f967e81 commit 2da6499
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions baekjoon/1988(WIP).js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 낮잠 시간 : 동적 계획법
const input = require("fs")
.readFileSync("/dev/stdin")
.toString()
.trim()
.split("\n");
const [n, b] = input
.shift()
.split(" ")
.map((v) => Number(v));
const fatigue = input.map((v) => Number(v));
const dp = new Array(2)
.fill(null)
.map(() => new Array(b + 1).fill(null).map(() => new Array(n + 1).fill(0)));

// col : n, row : b
for (let i = 1; i <= n; i++) {
for (let j = 1; j <= b; j++) {
dp[0][j][i] = Math.max(dp[0][j][i - 1], dp[1][j][i - 1]);
dp[1][j][i] = Math.max(
dp[1][j][i - 1],
j > 1 ? dp[0][j - 1][i - 1] + fatigue[i] : 0
);
}
}

console.log(dp);
console.log(dp[0][b][n], dp[1][b][n]);
// console.log(range);

0 comments on commit 2da6499

Please sign in to comment.