Skip to content

Commit 97da1ec

Browse files
[BOJ] 1149 RGB거리 (S1)
1 parent 77da9f4 commit 97da1ec

2 files changed

Lines changed: 33 additions & 3 deletions

File tree

서정우/11주차/260309.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const fs = require("fs");
2+
const filePath =
3+
process.platform === "linux" ? "/dev/stdin" : "./서정우/input.txt";
4+
const input = fs
5+
.readFileSync(filePath)
6+
.toString()
7+
.trim()
8+
.split("\n")
9+
.map((el) => el.trim());
10+
11+
const houseCount = Number(input[0]);
12+
const houses = input.slice(1).map((line) => line.split(" ").map(Number));
13+
14+
const dp = Array.from({ length: houseCount }, () => new Array(3).fill(0));
15+
16+
for (let i = 0; i < houseCount; i++) {
17+
const [r, g, b] = houses[i];
18+
if (i === 0) {
19+
dp[i][0] = r;
20+
dp[i][1] = g;
21+
dp[i][2] = b;
22+
} else {
23+
dp[i][0] = Math.min(dp[i - 1][1], dp[i - 1][2]) + r;
24+
dp[i][1] = Math.min(dp[i - 1][0], dp[i - 1][2]) + g;
25+
dp[i][2] = Math.min(dp[i - 1][0], dp[i - 1][1]) + b;
26+
}
27+
}
28+
29+
console.log(Math.min(...dp[houseCount - 1]));

서정우/input.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
2
2-
ABC-0123
3-
AAA-9999
1+
3
2+
26 40 83
3+
49 60 57
4+
13 89 99

0 commit comments

Comments
 (0)