๐ฆ ์ฝ๋ฉ ํ ์คํธ ๊ณต๋ถ
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 ๊ฐ ์๋ ๊ฒฝ์ฐ ๋ชจ๋ ๊ฒฝ์ฐ์์์ ๋ํ ๊ณ์ฐ ์ํ
๋ฒ์๋ฅผ ๋ฒ์ด๋์ง ์๋๋ก ๊ฐ์ ์กฐ์ ํ ํ์๊ฐ ์์