๐ฆ ์๋ด์ ์ธ์
1. ๋ฌธ์ ๋งํฌ
2. ์ฝ๋
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from itertools import product
def solution(k, n, reqs):
mentor = range(1, n + 1)
mentors = [p for p in product(mentor, repeat=k) if sum(p) == n]
min_wait_time = 10000000
# ๋ฉํ ๋ฐฐ์นํ๋ ๋ชจ๋ ๊ฒฝ์ฐ์ ์
for mentor_case in mentors:
# ์๋ด ๋๊ธฐํ
wating_queue = []
for i in range(k):
wating_queue.append([])
# n๋ฒ์งธ ์ผ์ด์ค ๊ฒ์ฌ
for i in range(len(mentor_case)):
# i+1 ๋ฒ ๋ฉํ ์ ๊ฑธ๋ฆฌ๋ ์๊ฐ ๊ณ์ฐ
mentor_index = i + 1
mentor_count = mentor_case[i] # ๋ฉํ ์
total_wait_time = 0
for start, time, number in reqs:
# ์ด ๋ฉํ ์ ํด๋นํ๋ ์๋ด ์ผ์ด์ค๋ฉด
if number == mentor_index:
# ์๋ด ํ ๊ฒ์ฌ
# ์์ง ์ฌ๋์ ๋ฐ์ ์ ์์ผ๋ฉด ์ ์ฅ
if len(wating_queue[i]) < mentor_count:
wating_queue[i].append([start, start + time])
# ์ฌ๋์ ๋ชป๋ฐ์ผ๋ฉด ์์ ๊ฒ์ ๋นผ๋ด๊ณ ์๊ฐ ๊ฒ์ฌ
else:
# ์ ์ผ ๋นจ๋ฆฌ ๋๋๋ ์์ผ๋ก ์ ์ฅ
wating_queue[i] = sorted(wating_queue[i], key=lambda x: x[1])
started, end = wating_queue[i].pop(0)
# ์์ ์๊ฐ์ด ์ ์ผ ๋นจ๋ฆฌ ๋๋ ์ฌ๋ ๋ณด๋ค ๋๋ฆฌ๋ฉด ๊ธฐ๋ค๋ฆผ
# ๊ธฐ๋ค๋ ธ๋ ์๊ฐ
if start < end:
wait_time = end - start
total_wait_time += wait_time
# ์ด์ ์ฌ๋์ด ๋๋ ์งํ๋ก ์ ์ฅ
wating_queue[i].append([end, end + time])
else:
wating_queue[i].append([start, start + time])
min_wait_time = min(min_wait_time, total_wait_time)
return min_wait_time
- ์ ํ์ฑ
3. ํด์ค
์ฌ๋์ ํ์
This post is licensed under CC BY 4.0 by the author.