Skip to content

Commit 1b8507f

Browse files
committed
solved(python): programmers
- 연습문제 / 호텔 대실
1 parent 696a079 commit 1b8507f

4 files changed

Lines changed: 122 additions & 0 deletions

File tree

programmers/python/연습문제/호텔_대실/__init__.py

Whitespace-only changes.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from typing import List
2+
3+
4+
def solution(book_time: List[List[str]]) -> int:
5+
times, rooms = (
6+
sorted(
7+
[(time_to_num(start), time_to_num(end) + 10) for start, end in book_time],
8+
key=lambda time: time[0],
9+
),
10+
[],
11+
)
12+
13+
for start, end in times:
14+
allocated = False
15+
16+
for idx, room_end in enumerate(rooms):
17+
if room_end <= start:
18+
rooms[idx] = end
19+
allocated = True
20+
break
21+
22+
if not allocated:
23+
rooms.append(end)
24+
25+
return len(rooms)
26+
27+
28+
def time_to_num(time: str) -> int:
29+
hour, minute = map(int, time.split(":"))
30+
31+
return hour * 60 + minute
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
[
2+
{
3+
"params": [
4+
[
5+
[
6+
"15:00",
7+
"17:00"
8+
],
9+
[
10+
"16:40",
11+
"18:20"
12+
],
13+
[
14+
"14:20",
15+
"15:20"
16+
],
17+
[
18+
"14:10",
19+
"19:20"
20+
],
21+
[
22+
"18:20",
23+
"21:20"
24+
]
25+
]
26+
],
27+
"expected": 3
28+
},
29+
{
30+
"params": [
31+
[
32+
[
33+
"09:10",
34+
"10:10"
35+
],
36+
[
37+
"10:20",
38+
"12:20"
39+
]
40+
]
41+
],
42+
"expected": 1
43+
},
44+
{
45+
"params": [
46+
[
47+
[
48+
"10:20",
49+
"12:30"
50+
],
51+
[
52+
"10:20",
53+
"12:30"
54+
],
55+
[
56+
"10:20",
57+
"12:30"
58+
]
59+
]
60+
],
61+
"expected": 3
62+
}
63+
]
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import json
2+
import os
3+
import unittest
4+
5+
from parameterized import parameterized
6+
7+
from .main import solution
8+
9+
10+
def load_sample(filename: str):
11+
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), filename)
12+
13+
with open(path, "r") as file:
14+
return [(case["params"], case["expected"]) for case in json.load(file)]
15+
16+
17+
class TestCase(unittest.TestCase):
18+
@parameterized.expand(load_sample("sample.json"))
19+
def test_case(self, params: list, expected: any):
20+
# When
21+
result = solution(*params)
22+
23+
# Then
24+
self.assertEqual(expected, result)
25+
26+
27+
if __name__ == "__main__":
28+
unittest.main()

0 commit comments

Comments
 (0)