Skip to content

Commit d98218b

Browse files
committed
solved(python): baekjoon 17404
1 parent 7488285 commit d98218b

4 files changed

Lines changed: 155 additions & 0 deletions

File tree

baekjoon/python/17404/__init__.py

Whitespace-only changes.

baekjoon/python/17404/main.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import math
2+
import sys
3+
4+
read = lambda: sys.stdin.readline().rstrip()
5+
6+
7+
class Problem:
8+
def __init__(self):
9+
self.n = int(read())
10+
self.data = [list(map(int, read().split())) for _ in range(self.n)]
11+
12+
def solve(self) -> None:
13+
print(min(*[self.find_min_cost(color) for color in range(3)]))
14+
15+
def find_min_cost(self, start_color: int) -> int:
16+
dp = [[math.inf for _ in range(3)] for _ in range(self.n)]
17+
dp[0][start_color] = self.data[0][start_color]
18+
19+
for row in range(1, self.n):
20+
dp[row][0] = min(dp[row - 1][1], dp[row - 1][2]) + self.data[row][0]
21+
dp[row][1] = min(dp[row - 1][0], dp[row - 1][2]) + self.data[row][1]
22+
dp[row][2] = min(dp[row - 1][0], dp[row - 1][1]) + self.data[row][2]
23+
24+
dp[-1][start_color] = math.inf
25+
return int(min(dp[-1]))
26+
27+
28+
if __name__ == "__main__":
29+
Problem().solve()

baekjoon/python/17404/sample.json

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
[
2+
{
3+
"input": [
4+
"3",
5+
"26 40 83",
6+
"49 60 57",
7+
"13 89 99"
8+
],
9+
"expected": [
10+
"110"
11+
]
12+
},
13+
{
14+
"input": [
15+
"3",
16+
"1 100 100",
17+
"100 1 100",
18+
"100 100 1"
19+
],
20+
"expected": [
21+
"3"
22+
]
23+
},
24+
{
25+
"input": [
26+
"3",
27+
"1 100 100",
28+
"100 100 100",
29+
"1 100 100"
30+
],
31+
"expected": [
32+
"201"
33+
]
34+
},
35+
{
36+
"input": [
37+
"6",
38+
"30 19 5",
39+
"64 77 64",
40+
"15 19 97",
41+
"4 71 57",
42+
"90 86 84",
43+
"93 32 91"
44+
],
45+
"expected": [
46+
"208"
47+
]
48+
},
49+
{
50+
"input": [
51+
"8",
52+
"71 39 44",
53+
"32 83 55",
54+
"51 37 63",
55+
"89 29 100",
56+
"83 58 11",
57+
"65 13 15",
58+
"47 25 29",
59+
"60 66 19"
60+
],
61+
"expected": [
62+
"253"
63+
]
64+
},
65+
{
66+
"input": [
67+
"4",
68+
"3 2 1",
69+
"1 2 2",
70+
"2 2 1",
71+
"2 1 2"
72+
],
73+
"expected": [
74+
"4"
75+
]
76+
},
77+
{
78+
"input": [
79+
"5",
80+
"9 2 8",
81+
"8 6 5",
82+
"4 8 5",
83+
"3 8 2",
84+
"6 8 8"
85+
],
86+
"expected": [
87+
"19"
88+
]
89+
}
90+
]

baekjoon/python/17404/test_main.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import json
2+
import os.path
3+
import unittest
4+
from io import StringIO
5+
from unittest.mock import patch
6+
7+
from parameterized import parameterized
8+
9+
from main import Problem
10+
11+
12+
def load_sample(filename: str):
13+
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), filename)
14+
15+
with open(path, "r") as file:
16+
return [(case["input"], case["expected"]) for case in json.load(file)]
17+
18+
19+
class TestCase(unittest.TestCase):
20+
@parameterized.expand(load_sample("sample.json"))
21+
def test_case(self, case: str, expected: list[str]):
22+
# When
23+
with (
24+
patch("sys.stdin.readline", side_effect=case),
25+
patch("sys.stdout", new_callable=StringIO) as output,
26+
):
27+
Problem().solve()
28+
29+
result = output.getvalue().rstrip()
30+
31+
# Then
32+
self.assertEqual("\n".join(expected), result)
33+
34+
35+
if __name__ == "__main__":
36+
unittest.main()

0 commit comments

Comments
 (0)