Post

๐Ÿฃ ์ƒ๋‹ด์› ์ธ์›

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
# FIFO
# ๊ฐ ๋ฉ˜ํ† ๋‹น ๋๋‚˜๋Š” ์‹œ๊ฐ„ ๊ธฐ๋ก
# ๋๋‚˜๋Š” ์‹œ๊ฐ„์ด ๋” ๋น ๋ฅธ ๋ฉ˜ํ† ์—๊ฒŒ ์ƒ๋‹ด ์ง„ํ–‰
# ์šฐ์„  ๋ฉ˜ํ†  ํ•œ๋ช…์”ฉ ๋ฐฐ์ •ํ•˜๊ณ  ์ดํ›„, ๊ธฐ๋‹ค๋ฆฌ๋Š” ์‹œ๊ฐ„์ด ๊ฐ€์žฅ ๊ธด ํŒ€์—๊ฒŒ ๋ฉ˜ํ†  ์ถ”๊ฐ€ ๋ฐฐ์ •

def min_time(start_time, end_time, group_queue):
    wait_time = 0
    min_v = int(1e9)
    target_idx = 0

    for i in range(len(group_queue)):
        if group_queue[i] <= start_time:
            target_idx = i
            min_v = 0
            break
        # ์‹œ๊ฐ„์ฐจ๊ฐ€ ์ตœ์†Œ๊ฐ€ ๋˜๋Š” ๊ฐ’ ์ฐพ๊ธฐ
        if min_v > group_queue[i] - start_time:
            target_idx = i
            min_v = group_queue[i] - start_time

    group_queue[target_idx] = max(start_time, group_queue[target_idx]) + end_time # ๋๋‚˜๋Š” ์‹œ๊ฐ„์œผ๋กœ ๋ฐ”๊ฟ”์คŒ
    wait_time = min_v

    return wait_time, group_queue


def solution(k, n, reqs):
    answer = 0
    wait_queue = [[0] for _ in range(k + 1)]
    n = n - k

    while n >= 0:
        wait_time = [0] * (k + 1) # ๊ฐ ๊ทธ๋ฃน๋ณ„๋กœ ๊ธฐ๋‹ค๋ ค์•ผ ํ•˜๋Š” ์‹œ๊ฐ„ ์ดํ•ฉ
        for start, end, g in reqs:
            t, q = min_time(start, end, wait_queue[g])
            wait_time[g] += t
            wait_queue[g] = q

        max_time = max(wait_time) # ๊ฐ ๊ทธ๋ฃน ์ค‘ ์ตœ๋Œ€๋กœ ๊ธฐ๋‹ค๋ ค์•ผํ•˜๋Š” ๊ทธ๋ฃน์˜ ์‹œ๊ฐ„
        wait_queue[wait_time.index(max_time)].append(0)
        n -= 1
        for i in range(k + 1):
            wait_queue[i] = len(wait_queue[i]) * [0]

    answer = sum(wait_time)
    return answer
This post is licensed under CC BY 4.0 by the author.