Skip to content

Commit 757fade

Browse files
Merge pull request #752 from eric-hjh/main
[황장현] 125차 라이브 코테 제출
2 parents 434c452 + 7517c9e commit 757fade

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const input = require('fs')
2+
.readFileSync(process.platform === 'linux' ? '/dev/stdin' : './input.txt')
3+
.toString()
4+
.trim()
5+
.split('\n')
6+
.map((el) => el.split(' ').map(Number));
7+
8+
const N = input[0][0];
9+
const home = input.slice(1);
10+
11+
let answer = Infinity;
12+
13+
// 0: R, 1: G, 2: B
14+
for (let firstColor = 0; firstColor < 3; firstColor++) {
15+
const dp = Array.from({ length: N }, () => Array(3).fill(Infinity));
16+
17+
dp[0][firstColor] = home[0][firstColor];
18+
19+
for (let i = 1; i < N; i++) {
20+
dp[i][0] = Math.min(dp[i - 1][1], dp[i - 1][2]) + home[i][0]; // R
21+
dp[i][1] = Math.min(dp[i - 1][0], dp[i - 1][2]) + home[i][1]; // G
22+
dp[i][2] = Math.min(dp[i - 1][0], dp[i - 1][1]) + home[i][2]; // B
23+
}
24+
25+
for (let lastColor = 0; lastColor < 3; lastColor++) {
26+
if (lastColor !== firstColor) {
27+
answer = Math.min(answer, dp[N - 1][lastColor]);
28+
}
29+
}
30+
}
31+
32+
console.log(answer);

0 commit comments

Comments
 (0)