Skip to content

Commit eb31108

Browse files
committed
solved(python): baekjoon 1006
1 parent 9bacc68 commit eb31108

4 files changed

Lines changed: 127 additions & 0 deletions

File tree

baekjoon/python/1006/__init__.py

Whitespace-only changes.

baekjoon/python/1006/main.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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, self.w = map(int, read().split())
10+
self.inner, self.outer = list(map(int, read().split())), list(map(int, read().split()))
11+
12+
def solve(self) -> None:
13+
if self.n == 1:
14+
print(2 - int(self.inner[0] + self.outer[0] <= self.w))
15+
return
16+
17+
minimum = min(
18+
self.find_minimum(
19+
[[1, 1, 2 - int(self.inner[0] + self.outer[0] <= self.w)]] + [[0, 0, 0] for _ in range(self.n - 1)],
20+
self.inner[:],
21+
self.outer[:],
22+
)[-1][2],
23+
(
24+
math.inf
25+
if self.inner[-1] + self.inner[0] > self.w
26+
else self.find_minimum(
27+
[[1, 1, 2]] + [[0, 0, 0] for _ in range(self.n - 1)],
28+
[math.inf] + self.inner[1:-1] + [math.inf],
29+
self.outer[:],
30+
)[-1][1]
31+
),
32+
(
33+
math.inf
34+
if self.outer[-1] + self.outer[0] > self.w
35+
else self.find_minimum(
36+
[[1, 1, 2]] + [[0, 0, 0] for _ in range(self.n - 1)],
37+
self.inner[:],
38+
[math.inf] + self.outer[1:-1] + [math.inf],
39+
)[-1][0]
40+
),
41+
(
42+
math.inf
43+
if (self.inner[-1] + self.inner[0] > self.w) or (self.outer[-1] + self.outer[0] > self.w)
44+
else self.find_minimum(
45+
[[1, 1, 2]] + [[0, 0, 0] for _ in range(self.n - 1)],
46+
[math.inf] + self.inner[1:-1] + [math.inf],
47+
[math.inf] + self.outer[1:-1] + [math.inf],
48+
)[-2][2]
49+
),
50+
)
51+
52+
print(minimum)
53+
54+
def find_minimum(self, dp: list[list[int]], inner: list[float], outer: list[float]) -> list[list[int]]:
55+
for idx in range(1, self.n):
56+
inner_count = 2 - int(inner[idx - 1] + inner[idx] <= self.w)
57+
outer_count = 2 - int(outer[idx - 1] + outer[idx] <= self.w)
58+
vertical_count = 2 - int(inner[idx] + outer[idx] <= self.w)
59+
60+
dp[idx] = [
61+
min(dp[idx - 1][1] + inner_count, dp[idx - 1][2] + 1),
62+
min(dp[idx - 1][0] + outer_count, dp[idx - 1][2] + 1),
63+
min(
64+
dp[idx - 2][2] + inner_count + outer_count,
65+
dp[idx - 1][0] + outer_count + 1,
66+
dp[idx - 1][1] + inner_count + 1,
67+
dp[idx - 1][2] + vertical_count,
68+
),
69+
]
70+
71+
return dp
72+
73+
74+
if __name__ == "__main__":
75+
for _ in range(int(read())):
76+
Problem().solve()

baekjoon/python/1006/sample.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[
2+
{
3+
"input": [
4+
"1",
5+
"8 100",
6+
"70 60 55 43 57 60 44 50",
7+
"58 40 47 90 45 52 80 40"
8+
],
9+
"expected": [
10+
"11"
11+
]
12+
}
13+
]

baekjoon/python/1006/test_main.py

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

0 commit comments

Comments
 (0)