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
47
INF = int(1e9)

def solution(alp, cop, problems):
    problems.sort(key=lambda x : -x[0])
    min_alp = max(problems[0][0], alp)
    problems.sort(key=lambda x : -x[1])
    min_cop = max(problems[0][1], cop)
    
    dp = [[INF] * 151 for _ in range(151)]
    dp[alp][cop] = 0
    
    # ๊ณต๋ถ€ ์ด์ „์— ์ด๋ฏธ ๋ชจ๋“  ๋ฌธ์ œ ํ’€ ์ˆ˜ ์žˆ์„ ๋•Œ
    if alp >= min_alp and cop >= min_cop:
        return 0
    
    def can_solve(now_alp, now_cop, p):
        a, c, ua, uc, cost = p
        return now_alp >= a and now_cop >= c
    
    def possible_alp_index(idx):
        if idx > min_alp:
            return min_alp
        else:
            return idx
        
    def possible_cop_index(idx):
        if idx > min_cop:
            return min_cop
        else:
            return idx
    
    for i in range(alp, min_alp + 1):
        for j in range(cop, min_cop + 1):
            # ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ณต๋ถ€ํ•˜์—ฌ ์•Œ๊ณ ๋ ฅ 1 ์˜ฌ๋ฆฌ๊ธฐ
            alp_idx = possible_alp_index(i+1)
            dp[alp_idx][j] = min(dp[alp_idx][j], dp[i][j] + 1)
            # ์ฝ”๋”ฉ์„ ๊ณต๋ถ€ํ•˜์—ฌ ์ฝ”๋”ฉ๋ ฅ 1 ์˜ฌ๋ฆฌ๊ธฐ
            cop_idx = possible_cop_index(j+1)
            dp[i][cop_idx] = min(dp[i][cop_idx], dp[i][j] + 1)
            # ๋ฌธ์ œ๋ฅผ ํ’€์–ด ์•Œ๊ณ ๋ ฅ, ์ฝ”๋”ฉ๋ ฅ ์˜ฌ๋ฆฌ๊ธฐ
            for p in problems:
                a, c, ua, uc, cost = p
                if can_solve(i, j, p):
                    alp_idx, cop_idx = possible_alp_index(i+ua), possible_cop_index(j+uc)
                    dp[alp_idx][cop_idx] = min(dp[alp_idx][cop_idx], dp[i][j] + cost)
    
    return dp[min_alp][min_cop]
  • ์ •ํ™•์„ฑ
ํ…Œ์ŠคํŠธ 1 ใ€‰ํ†ต๊ณผ (0.62ms, 10.4MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰ํ†ต๊ณผ (0.66ms, 10.4MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰ํ†ต๊ณผ (0.36ms, 10.4MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰ํ†ต๊ณผ (0.19ms, 10.4MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰ํ†ต๊ณผ (0.34ms, 10.4MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰ํ†ต๊ณผ (0.43ms, 10.4MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰ํ†ต๊ณผ (0.20ms, 10.3MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰ํ†ต๊ณผ (0.20ms, 10.5MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰ํ†ต๊ณผ (0.28ms, 10.4MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰ํ†ต๊ณผ (0.19ms, 10.5MB)
  • ํšจ์œจ์„ฑ
ํ…Œ์ŠคํŠธ 1 ใ€‰ํ†ต๊ณผ (34.96ms, 10.4MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰ํ†ต๊ณผ (30.26ms, 10.5MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰ํ†ต๊ณผ (4.83ms, 10.5MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰ํ†ต๊ณผ (23.22ms, 10.2MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰ํ†ต๊ณผ (171.49ms, 10.4MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰ํ†ต๊ณผ (40.59ms, 10.5MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰ํ†ต๊ณผ (210.64ms, 10.4MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰ํ†ต๊ณผ (162.95ms, 10.4MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰ํ†ต๊ณผ (384.07ms, 10.3MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰ํ†ต๊ณผ (5.62ms, 10.3MB)


3. ํ•ด์„ค

๋ฌธ์ œ๋ฅผ ๋ชจ๋‘ ํ’€๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฌธ์ œ๋“ค ์ค‘ ํ•„์š”ํ•œ ์ตœ๋Œ€ ์•Œ๊ณ ๋ ฅ๊ณผ ์ตœ๋Œ€ ์ฝ”๋”ฉ๋ ฅ์„ ๋งŒ์กฑํ•ด์•ผ ํ•œ๋‹ค.

๊ฐ ์•Œ๊ณ ๋ ฅ๊ณผ ์ฝ”๋”ฉ๋ ฅ์— ๋„๋‹ฌํ–ˆ์„ ๋•Œ์˜ ์ตœ์†Œ ์‹œ๊ฐ„์„ dp์— ์ €์žฅํ•œ๋‹ค.

โ‡’ โ€˜dp[์•Œ๊ณ ๋ ฅ][์ฝ”๋”ฉ๋ ฅ] = ๊ฑธ๋ฆฌ๋Š” ์ตœ์†Œ ์‹œ๊ฐ„โ€™

์ฒ˜์Œ ์•Œ๊ณ ๋ ฅ, ์ฝ”๋”ฉ๋ ฅ์ผ ๋•Œ๋Š” 0์œผ๋กœ ์ดˆ๊ธฐํ™” ํ•ด์ฃผ๊ณ  ๋‚˜๋จธ์ง€ dp ๊ฐ’์€ ๋ฌดํ•œ๋Œ€๋กœ ์„ค์ •ํ•ด์ค€๋‹ค.

3๊ฐ€์ง€ ๊ฒฝ์šฐ๋กœ ์•Œ๊ณ ๋ ฅ, ์ฝ”๋”ฉ๋ ฅ์„ ๋†’์ผ ์ˆ˜ ์žˆ๋‹ค.

  1. ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ณต๋ถ€๋ฅผ ํ•˜์—ฌ ์•Œ๊ณ ๋ ฅ 1 ์˜ฌ๋ฆฌ๊ธฐ

    โ†’ dp[i+1][j] = min(dp[i+1][j], dp[i][j] + 1)

  2. ์ฝ”๋”ฉ ๊ณต๋ถ€๋ฅผ ํ•˜์—ฌ ์ฝ”๋”ฉ๋ ฅ 1 ์˜ฌ๋ฆฌ๊ธฐ

    โ†’ dp[i][j+1] = min(dp[i][j+1], dp[i][j] + 1)

  3. ๋ฌธ์ œ๋ฅผ ํ’€์–ด ์•Œ๊ณ ๋ ฅ, ์ฝ”๋”ฉ๋ ฅ ์˜ฌ๋ฆฌ๊ธฐ

    ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ์„ ๋•Œ ์˜ค๋ฅด๋Š” ์•Œ๊ณ ๋ ฅ : ua

    ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ์„ ๋•Œ ์˜ค๋ฅด๋Š” ์ฝ”๋”ฉ๋ ฅ : uc

    ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ์„ ๋•Œ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„ : cost

    โ†’ dp[i+ua][j+uc] = min(dp[i+ua][j+uc], dp[i][j] + cost)

๊ฐ ๊ฒฐ๊ณผ๋ฅผ dp์— ์ €์žฅํ•˜๊ณ  ์ด๋•Œ ๋ชจ๋“  ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์œ„ํ•œ ์ตœ์†Œ ์‹œ๊ฐ„์€ dp[๋ชฉํ‘œ ์•Œ๊ณ ๋ ฅ][๋ชฉํ‘œ ์ฝ”๋”ฉ๋ ฅ]์ด๋‹ค.

โญย ์ด๋•Œ (์ฒ˜์Œ ์•Œ๊ณ ๋ ฅ, ์ฒ˜์Œ ์ฝ”๋”ฉ๋ ฅ)์ด (๋ชฉํ‘œ ์•Œ๊ณ ๋ ฅ, ๋ชฉํ‘œ ์ฝ”๋”ฉ๋ ฅ)๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ์— ๋” ํฐ ๊ฐ’์„ ๋ชฉํ‘œ ๊ฐ’์œผ๋กœ ๋ณ€ํ˜•ํ•˜๊ณ  dp๊ฐ’์„ ์ดˆ๊ธฐํ™”ํ•ด์•ผํ•œ๋‹ค.

โญย dp์— ์•Œ๊ณ ๋ ฅ, ์ฝ”๋”ฉ๋ ฅ์˜ ์ธ๋ฑ์Šค๋กœ ์ ‘๊ทผํ•  ๋•Œ ๋ชฉํ‘œ๋ฅผ ๋„˜์–ด๊ฐ€๊ฑฐ๋‚˜ ์ •ํ•ด์ง„ ํฌ๊ธฐ๋ณด๋‹ค ๋” ํฐ ๊ฐ’์œผ๋กœ ์ ‘๊ทผํ•  ๊ฒฝ์šฐ ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ (๋ชฉํ‘œ ์•Œ๊ณ ๋ ฅ, ๋ชฉํ‘œ ์ฝ”๋”ฉ๋ ฅ)๋ณด๋‹ค ํฐ ์ธ๋ฑ์Šค๋กœ ์ ‘๊ทผํ•  ๊ฒฝ์šฐ๋ฅผ ๋ง‰๊ธฐ ์œ„ํ•ด ์ธ๋ฑ์Šค๊ฐ€ ๋” ํด ๊ฒฝ์šฐ ๋ชฉํ‘œ ์•Œ๊ณ ๋ ฅ, ๋ชฉํ‘œ ์ฝ”๋”ฉ๋ ฅ์œผ๋กœ ๋ฐ”๊พธ์–ด์ค€ ํ›„ ๊ณ„์‚ฐํ•œ๋‹ค.

This post is licensed under CC BY 4.0 by the author.