Skip to content

Commit

Permalink
07.08: dp(양팔저울)
Browse files Browse the repository at this point in the history
  • Loading branch information
luckylooky2 committed Jul 8, 2024
1 parent 595f434 commit bdadd1a
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions baekjoon/2629.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// 양팔저울 : 동적 계획법, 배낭 문제
const input = require("fs")
.readFileSync("/dev/stdin")
.toString()
.trim()
.split("\n")
.map((v) => v.split(" ").map((v) => Number(v)));
const [weightCount] = input[0];
const weights = input[1];
const [ballCount] = input[2];
const balls = input[3];
const totalWeight = weights.reduce((acc, curr) => acc + curr);
const dp = new Array(2)
.fill(null)
.map(() =>
new Array(weightCount + 1)
.fill(null)
.map(() => new Array(totalWeight + 1).fill(0))
);
const dptable = dp[0];
const visited = dp[1];
const answer = [];

for (let i = 1; i <= weightCount; i++) {
const curr = weights[i - 1];
for (let j = 1; j <= totalWeight; j++) {
// 현재 추를 사용하지 않는 경우
if (dptable[i - 1][j]) {
dptable[i][j] = 1;
visited[i][j] = 0;
}
// 현재 추를 사용하는 경우
if (j === curr) {
if (dptable[i][j]) {
// 이미 완성된 경우
if (j + curr <= totalWeight) {
dptable[i][j + curr] = 1;
visited[i][j + curr] = 1;
}
} else {
// 이미 완성되지 않은 경우
dptable[i][j] = 1;
visited[i][j] = 1;
}
} else {
// 아직 추를 사용하지 않았으므로, 현재 + curr 만큼의 무게도 잴 수 있다
if (dptable[i][j] && !visited[i][j] && j + curr <= totalWeight) {
dptable[i][j + curr] = 1;
visited[i][j + curr] = 1;
}
// 아직 추를 사용하지 않았으므로, Math.abs(현재 - curr) 만큼의 무게도 잴 수 있다
const diff = Math.abs(curr - j);
if (dptable[i][j] && !visited[i][j] && diff <= totalWeight) {
dptable[i][diff] = 1;
visited[i][diff] = 1;
}
}
}
}

for (const ball of balls) {
answer.push(dptable[weightCount][ball] ? "Y" : "N");
}

console.log(answer.join(" "));

0 comments on commit bdadd1a

Please sign in to comment.