πΉ μλ΄μ μΈμ
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from itertools import product
from collections import deque
def check_consult_finish(consult_list, now_category, req_time):
c_list = consult_list.copy()
finish = 0
while c_list:
end_time, category = c_list.popleft()
# μ΄μ μλ΄μ νμ λͺ¨λ μλ΄ λ§λ¬΄λ¦¬ -> μλ΄ λ΄μ μμ κΈ°
if category != now_category:
return (0, deque([]))
# νμ¬ μμ μμ μ΄λ―Έ λλ μλ΄ νμΈ
if end_time <= req_time:
finish += 1
continue
break
cnt = finish
while cnt:
cnt -= 1
consult_list.popleft()
return (finish, consult_list)
def solution(k, n, reqs):
# λ©ν nλͺ
mentor = range(1, n+1)
reqs.sort(key=lambda x: x[2])
# kκ°μ μ νμΌλ‘ λλλ λͺ¨λ κ²½μ°μ μ ꡬνκΈ° (μ€λ³΅μμ΄)
consulting_permutations = [p for p in product(mentor, repeat=k) if sum(p) == n]
min_value = int(1e9)
# λ©ν μλ΄ λ°°μΉ λͺ¨λ κ²½μ°
for c in consulting_permutations:
waiting = 0
mentors = list(c)
consult_list = deque([])
for req in reqs:
req_time, consult_time, category = req
# κ°μ₯ 빨리 λλλ μμΌλ‘ λκΈ°
consult_list = deque(sorted(consult_list, key=lambda x: x[0]))
# λ©ν μλ΄ λλ¬μΌλ©΄ λλ €λκΈ°
cnt, c_list = check_consult_finish(consult_list, category, req_time)
mentors[category-1] += cnt
consult_list = c_list
# λ°°μ ν λ©ν κ° μμ μ(μλ΄ λ°λ‘ κ°λ₯)
if mentors[category-1] > 0:
mentors[category-1] -= 1
consult_list.append((req_time + consult_time, category)) # μλ΄ λ°°μ
# λ°°μ ν λ©ν κ° μμ μ(λκΈ° νμ)
else:
end_time, category = consult_list.popleft()
waiting += end_time - req_time
consult_list.append((end_time + consult_time, category)) # μλ΄ λ°°μ
min_value = min(min_value, waiting)
return min_value
ν μ€νΈ 1 γ | ν΅κ³Ό (0.03ms, 10.5MB) |
---|---|
ν μ€νΈ 2 γ | ν΅κ³Ό (0.04ms, 10.4MB) |
ν μ€νΈ 3 γ | ν΅κ³Ό (0.04ms, 10.3MB) |
ν μ€νΈ 4 γ | ν΅κ³Ό (0.05ms, 10.4MB) |
ν μ€νΈ 5 γ | ν΅κ³Ό (1.47ms, 10.1MB) |
ν μ€νΈ 6 γ | ν΅κ³Ό (20.61ms, 10.3MB) |
ν μ€νΈ 7 γ | ν΅κ³Ό (6.40ms, 10.3MB) |
ν μ€νΈ 8 γ | ν΅κ³Ό (0.80ms, 10.3MB) |
ν μ€νΈ 9 γ | ν΅κ³Ό (1935.87ms, 10.6MB) |
ν μ€νΈ 10 γ | ν΅κ³Ό (2103.60ms, 10.6MB) |
ν μ€νΈ 11 γ | ν΅κ³Ό (1921.13ms, 10.6MB) |
ν μ€νΈ 12 γ | ν΅κ³Ό (1881.78ms, 10.5MB) |
ν μ€νΈ 13 γ | ν΅κ³Ό (1908.63ms, 10.5MB) |
ν μ€νΈ 14 γ | ν΅κ³Ό (2061.11ms, 10.5MB) |
ν μ€νΈ 15 γ | ν΅κ³Ό (2001.30ms, 10.5MB) |
ν μ€νΈ 16 γ | ν΅κ³Ό (1968.97ms, 10.5MB) |
ν μ€νΈ 17 γ | ν΅κ³Ό (1956.36ms, 10.5MB) |
ν μ€νΈ 18 γ | ν΅κ³Ό (2097.11ms, 10.6MB) |
ν μ€νΈ 19 γ | ν΅κ³Ό (2034.02ms, 10.7MB) |
ν μ€νΈ 20 γ | ν΅κ³Ό (1925.74ms, 10.5MB) |
3. ν΄μ€
product(μ€λ³΅ μμ΄)μ μ¬μ©νμ¬ κ° μλ΄ μ νμ λ©ν λ₯Ό λ°°μΉνλ λͺ¨λ κ²½μ° consulting_permutationsλ₯Ό ꡬνλ€. κ° μλ΄ μ νλ³λ‘ κ³μ°νκΈ° μν΄ reqλ₯Ό μλ΄ μ νμμΌλ‘ μ λ ¬νλ€. κ·Έ ν λ€μκ³Ό κ°μ μ°μ°μ νλ€.
- νμ¬ μλ΄ λ¦¬μ€νΈ(
(λλλ μκ°, μλ΄ μ ν)
νμ ν)μ λ±λ‘λμ΄ μλ κ²½μ°λ₯Ό μλ΄μ΄ 빨리 λλλ μμΌλ‘ μ λ ¬νλ€. μλ΄μ΄ λλ λ©ν λ λκΈ° μ리μ λλ €λλλ€(μλ΄ μ ν(
i
)μ ν΄λΉνλ mentors[i]μ 1μ λν΄μ€)μ΄λ, λ§μ½ μλ΄ μ νμ΄ νμ¬ νμΈνλ μλ΄ μ νκ³Ό λ€λ₯΄λ©΄(μ΄μ λ²νΈ μλ΄μ΄ λͺ¨λ λλ¬λ€λ©΄) νλ₯Ό λΉμμ€λ€.
- μλ΄ λκΈ° μ 무λ₯Ό νμ
νλ€
μλ΄ λ°λ‘ κ°λ₯ β mentors[i]κ° 0 λ³΄λ€ ν¬λ€(μλ΄ κ°λ₯ν λ©ν κ° λ¨μμλ€)
μ΄λλ λ©ν μλ₯Ό νλ μ€μΈλ€.
λλλ μκ° = μλ΄ μμ²μκ° + μλ΄ μκ°
μΌλ‘ μ΄κΈ°ννμ¬ μλ΄ λ¦¬μ€νΈμ λ£μ΄μ€λ€.μλ΄ λκΈ° νμ β mentors[i]κ° 0μΌ λ(μλ΄ κ°λ₯ν λ©ν κ° μμ)
λκΈ° ν λ°λ‘ λ€μ λ©ν λ‘ λκΈ°κΈ° λλ¬Έμ λ©ν μλ κ·Έλλ‘ λλλ€.
κΈ°λ€λ¦° μκ° = μ΄μ μλ΄ λλλ μκ° - μλ΄ μμ² μκ°
μΌλ‘ μ΄ λκΈ° μκ°μ λν΄μ€λ€.λλλ μκ° = μ΄μ μλ΄ λλλ μκ° + μλ΄ μκ°
μΌλ‘ μ΄κΈ°ννμ¬ μλ΄ λ¦¬μ€νΈμ λ£μ΄μ€λ€.
μμ μ°μ°μ λ§λ¬΄λ¦¬ νμ λ λκΈ° μκ°μ μ΅μκ°μ 리ν΄νλ€.