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
def solution(alp, cop, problems):
    # dp[151][151] -> alp = 151, cop = 151 ์ด ๋˜๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„

    problems.sort(key=lambda x : -x[0])
    req_alp = max(problems[0][0], alp)
    problems.sort(key=lambda x : -x[1])
    req_cop = max(problems[0][1], cop)

    # ๋ฌดํ•œ๋Œ€๋กœ ์ดˆ๊ธฐํ™”
    INF = 200*200
    dp = []
    for i in range(151):
        dp.append([INF] * (151))

    # ํ˜„์žฌ ๋ ˆ๋ฒจ์„ 0์œผ๋กœ ์ดˆ๊ธฐํ™”
    dp[alp][cop] = 0

    for i in range(alp, req_alp + 1):
        for j in range(cop, req_cop + 1):
            # ๊ฐ’์ด ์กด์žฌํ•˜๋ฉด ๊ณ„์‚ฐ
            if dp[i][j] != INF:
                # ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ณต๋ถ€
                next_i = min(req_alp, i + 1)
                dp[next_i][j] = min(dp[i][j] + 1, dp[next_i][j])
                # ์ฝ”๋”ฉ๋ ฅ ๊ณต๋ถ€
                next_j = min(req_cop, j + 1)
                dp[i][next_j] = min(dp[i][j] + 1, dp[i][next_j])
                # ๋ฌธ์ œ ํ’€๊ธฐ
                for problem in problems:
                    alp_req, cop_req, alp_rwd, cop_rwd, cost = problem
                    
                    # ๋ฌธ์ œ๋ฅผ ํ’€์ˆ˜ ์žˆ๋Š” ๋ ˆ๋ฒจ์ด๋ฉด
                    if i >= alp_req and j >= cop_req:
                        # ๋ฒ”์œ„๋ฅผ ์ดˆ๊ณผํ•˜์ง€ ์•Š๋Š” ์„ ์—์„œ ๋ ˆ๋ฒจ ์ฆ๊ฐ€
                        next_alp = min(req_alp, i + alp_rwd)
                        next_cop = min(req_cop, j + cop_rwd)
                        dp[next_alp][next_cop] = min(dp[i][j] + cost, dp[next_alp][next_cop])

    return dp[req_alp][req_cop]
  • ์ •ํ™•์„ฑ

    ์ฑ„์ ์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. ์ •ํ™•์„ฑ ํ…Œ์ŠคํŠธ ํ…Œ์ŠคํŠธ 1 ใ€‰ ํ†ต๊ณผ (0.37ms, 10.5MB) ํ…Œ์ŠคํŠธ 2 ใ€‰ ํ†ต๊ณผ (0.68ms, 10.3MB) ํ…Œ์ŠคํŠธ 3 ใ€‰ ํ†ต๊ณผ (0.36ms, 10.3MB) ํ…Œ์ŠคํŠธ 4 ใ€‰ ํ†ต๊ณผ (0.26ms, 10.3MB) ํ…Œ์ŠคํŠธ 5 ใ€‰ ํ†ต๊ณผ (0.32ms, 10.4MB) ํ…Œ์ŠคํŠธ 6 ใ€‰ ํ†ต๊ณผ (0.38ms, 10.3MB) ํ…Œ์ŠคํŠธ 7 ใ€‰ ํ†ต๊ณผ (0.24ms, 10.3MB) ํ…Œ์ŠคํŠธ 8 ใ€‰ ํ†ต๊ณผ (0.20ms, 10.5MB) ํ…Œ์ŠคํŠธ 9 ใ€‰ ํ†ต๊ณผ (0.24ms, 10.3MB) ํ…Œ์ŠคํŠธ 10 ใ€‰ ํ†ต๊ณผ (0.20ms, 10.4MB) ํšจ์œจ์„ฑ ํ…Œ์ŠคํŠธ ํ…Œ์ŠคํŠธ 1 ใ€‰ ํ†ต๊ณผ (20.62ms, 10.3MB) ํ…Œ์ŠคํŠธ 2 ใ€‰ ํ†ต๊ณผ (27.08ms, 10.4MB) ํ…Œ์ŠคํŠธ 3 ใ€‰ ํ†ต๊ณผ (3.81ms, 10.3MB) ํ…Œ์ŠคํŠธ 4 ใ€‰ ํ†ต๊ณผ (20.10ms, 10.4MB) ํ…Œ์ŠคํŠธ 5 ใ€‰ ํ†ต๊ณผ (135.45ms, 10.5MB) ํ…Œ์ŠคํŠธ 6 ใ€‰ ํ†ต๊ณผ (33.53ms, 10.4MB) ํ…Œ์ŠคํŠธ 7 ใ€‰ ํ†ต๊ณผ (165.69ms, 10.3MB) ํ…Œ์ŠคํŠธ 8 ใ€‰ ํ†ต๊ณผ (138.94ms, 10.3MB) ํ…Œ์ŠคํŠธ 9 ใ€‰ ํ†ต๊ณผ (331.87ms, 10.4MB) ํ…Œ์ŠคํŠธ 10 ใ€‰ ํ†ต๊ณผ (4.76ms, 10.4MB) ์ฑ„์  ๊ฒฐ๊ณผ ์ •ํ™•์„ฑ: 50.0 ํšจ์œจ์„ฑ: 50.0 ํ•ฉ๊ณ„: 100.0 / 100.0


3. ํ•ด์„ค

  • ํ•ด์„ค

    dp[i][j] โ‡’ alp = i, cop = j ๊ฐ€ ๋˜๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„

    ์ดˆ๊ธฐ๊ฐ’ ๋ถ€ํ„ฐ ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉด ๊ฐ’์ด INF ๊ฐ€ ์•„๋‹ ๊ฒฝ์šฐ ๋ชจ๋“  ๊ฒฝ์šฐ์˜์ˆ˜์— ๋Œ€ํ•œ ๊ณ„์‚ฐ ์ˆ˜ํ–‰

    ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜์ง€ ์•Š๋„๋ก ๊ฐ’์„ ์กฐ์ •ํ•  ํ•„์š”๊ฐ€ ์žˆ์Œ

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