Skip to content

Commit b18025d

Browse files
committed
solved(python): baekjoon 6549
1 parent 98772fc commit b18025d

4 files changed

Lines changed: 83 additions & 0 deletions

File tree

baekjoon/python/6549/__init__.py

Whitespace-only changes.

baekjoon/python/6549/main.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import sys
2+
3+
read = lambda: sys.stdin.readline().rstrip()
4+
5+
6+
class Problem:
7+
def __init__(self):
8+
self.cases = []
9+
10+
while True:
11+
case = list(map(int, read().split()))
12+
if case[0] == 0:
13+
break
14+
15+
self.cases.append(case[1:])
16+
17+
def solve(self) -> None:
18+
for case in self.cases:
19+
stack, case, maximum, index = [], case + [0], 0, 0
20+
21+
while index < len(case):
22+
if not stack or case[stack[-1]] <= case[index]:
23+
stack.append(index)
24+
index += 1
25+
else:
26+
top = stack.pop()
27+
width, height = index if not stack else index - stack[-1] - 1, case[top]
28+
maximum = max(maximum, width * height)
29+
30+
print(maximum)
31+
32+
33+
if __name__ == "__main__":
34+
Problem().solve()

baekjoon/python/6549/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+
"7 2 1 4 5 1 3 3",
5+
"4 1000 1000 1000 1000",
6+
"0"
7+
],
8+
"expected": [
9+
"8",
10+
"4000"
11+
]
12+
}
13+
]

baekjoon/python/6549/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)