DPλ μ νμ λ§μ΄ μ νλκ² μ€μνλ€κ³ νλ€.....π
2μ°¨μ λ°°μ΄μ κ°λ°©μ λ€μ΄κ° μ μλ 무κ²λ₯Ό λλ €κ°λ©° λͺ¨λ κ²½μ°μ μ λΉκ΅.
물건μ κ°λ°©μ λ£μ μ μλ€λ©΄, μ§κΈκΉμ§ 물건 μ€ μ΅λ κ°μΉ
λ£μ μ μλ€λ©΄, μ§κΈ 물건μ κ°μΉ + μ νλ ¬μμ μ§κΈ 무κ²λ₯Ό λΊμ λμ κ°μΉ
def knapsack():
dp = [[0 for _ in range(K+1)] for _ in range(N+1)]
for i in range(1,N+1):
for j in range(1,K+1):
if A[i][0] > j:
dp[i][j] = dp[i-1][j]
else:
dp[i][j] = max(dp[i-1][j],dp[i-1][j-A[i][0]]+A[i][1])
print(dp[-1][-1])
2μ°¨μ λ°°μ΄μ ν λ¨μ΄μ© λλ €κ°λ©° λͺ¨λ κ²½μ°μ μλ₯Ό λΉκ΅.
μλ‘ μ
λ ₯λλ λ¨μ΄κ° κ°λ€λ©΄, μ λ°°μ΄μ +1
κ·Έλ μ§ μλ€λ©΄, μ§κΈκΉμ§ λ§λ λ¨μ΄ μ€ μ΅λ
def max_lcs():
N = len(s1)
M = len(s2)
lcs = [[0 for _ in range(M+1)] for _ in range(N+1)]
for i in range(1,N+1):
for j in range(1,M+1):
if s1[i-1] == s2[j-1]:
lcs[i][j] = lcs[i-1][j-1] + 1
else:
lcs[i][j] = max(lcs[i-1][j],lcs[i][j-1])
print(lcs[-1][-1])
λͺ¨λ νλ ¬κ³±μ
μ κ²½μ°μ μλ₯Ό κ³±ν΄μ€λ€.
μ λμ κ³ μ μ΄λ μ€κ°μ μ΄λ»κ² μͺΌκ°λλλ‘ κ²½μ°μ μκ° λλλ κ²μ μ΄μ©νλ€.
A,B,C,D,E,F λ₯Ό μλ‘ λ€λ©΄,
(A)(B,C,D,E,F)
(A,B) (C,D,E,F)
(A,B,C)(D,E,F)
(A,B,C,D)(E,F)
(A,B,C,D,E)(F)
κ° λͺ¨λ κ²½μ°μ μ μΌ κ²μΈλ€. λ€λ§ λ¬ΆμΈ μ°μ°μ μ΅μκ°μ μ΄λ―Έ ꡬν΄μ Έ μμ΄μΌ ν κ²μ΄λ€.
2μ°¨μ λ°°μ΄μμ κ³μ°λλ μμμ λͺ¨μμ 보면 λͺ¨λ μ κ²½μ°μ μκ° κ³ λ €λκ³ μμμ μ’λ μ§κ΄μ μΌλ‘ λ³Ό μ μλ€.
μ£Όμκ³Ό μ νμμ μ보μ
dp = [[0 for _ in range(N)] for _ in range(N)]
for d in range(1,N): # μΌλ§λ§νΌμ ν¬κΈ°λ‘ μͺΌκ°€ κ²μΈκ°
for i in range(N-d): # μμμ
j = i+d # λμ
dp[i][j] = math.inf
for k in range(i,i+d): # λλλ μ
dp[i][j] = min(dp[i][j], dp[i][k]+dp[k+1][j]+A[i][0]*A[k][1]*A[j][1])
print(dp[0][N-1])
DFSμ²λΌ μ¬κ·λ‘ μμ νμν΄ μ΅μκ°μ μ·¨νλ€.
μ¬κ· μ€μ ꡬ체μ μΈ κ²½λ‘λ μ€μνμ§ μμμΌλ‘ λΉνΈ μ°μ°μ μ¬μ©
μ€λ³΅λμ§ μλλ‘ μ¬λ°©λ¬Έμ λ§λλ€
https://developmentdiary.tistory.com/406
https://shoark7.github.io/programming/algorithm/solve-tsp-with-dynamic-programming
dp = [[None]*(1<<N) for _ in range(N)]
def find_path(last,visited):
if visited == (1<<N)-1 # λͺ¨λ λ°©λ¬Έ μ
return dp[last][0] or math.inf # 0μΌλλ math.inf
if dp[last][visited] is not None: # μ€λ³΅ λ°©λ¬Έ μ
return dp[last][visited]
tmp = math.inf
for i in range(N):
# λ°©λ¬Ένμ μ΄ μκ³ κ° μ μλ€λ©΄ κ·Έ μ€ μ΅μκ°
if visited & (1<<i) == 0 and A[last][i] != 0:
tmp = min(tmp,find_path(i,visited | (1<<i)) + A[last][[i])
dp[last][visted] = tmp
return tmp
2μ°¨μ λ°°μ΄μ μ΄μ©. μΈ μΉΈμ μ°μν΄μ μ νν μ μλ€. μ¦, νμΉΈ λ°μλ€λ©΄ λ€μλ²μλ λ μΉΈμ λ°μ΄μΌνλ€.
κ·Έλμ Nμ λν΄ λ κ°μ λ°°μ΄μ μ¨μ A[N][0]μ λ μΉΈ λ΄ κ° A[N][1]μ ν μΉΈ λ΄ κ°μ λ£μ΄μ€λ€.
dp = [[0,0] for _ in range(N)]
dp[0] = [A[0],0] # μ΄κΈ°κ°μ λ£μ΄μ€μΌ
dp[1] = [A[1],A[1]+A[0]]
for i in range(2,N):
dp[i][0] = max(dp[i-2][0],dp[i-2][1]) + A[i]
dp[i][1] = dp[i-1][0] + A[i]
print(max(dp[N-1][0],dp[N-1][1]))
Nx3λ°°μ΄μ μ§μ© λλ €κ°λ©° λͺ¨λ κ²½μ°μ μλ₯Ό λΉκ΅.
μ μ μ¬μ©λ μκΉμ λΊ λλ¨Έμ§ λ κ°μ μμ λν΄ μ΅μκ°μ μ°μ°
dp = [[0,0,0] for _ in range(N)]
dp[0] = [A[0][0],A[0][1],A[0][2]]
for i in range(1,N):
dp[i][0] = min(dp[i-1][1]+A[i][0],dp[i-1][2]+A[i][0])
dp[i][1] = min(dp[i-1][0]+A[i][1],dp[i-1][2]+A[i][1])
dp[i][2] = min(dp[i-1][0]+A[i][2],dp[i-1][1]+A[i][2])
print(min(dp[N-1][0],dp[N-1][1],dp[N-1][2]))
μ λ§ κ°λ¨ν λ¬Έμ μλλ° νμ°Έ κ³ λ―Όν λ¬Έμ .
1μ°¨μ dp μ μμκ° μλ μ°μ μλ€λ§ λͺ¨λ λν¨.
dp = [0 for _ in range(N)]
for i in range(0,N):
dp[i]= max(0,dp[i-1])+A[i]
print(max(dp))
νΌλ³΄λμΉ μμ΄λ‘ ν리λ λ¬Έμ κ° κ½€ μλ€.
dp = [0 for _ in range(N)]
dp[0], dp[1] = 1,2
for i in range(2,N):
dp[i] = (dp[i-1] + dp[i-2])
print(dp[N-1])
knapsack μ²λΌ λμ μ λͺ¨λ λμ μ κ°μΉμ λν΄ λμ μ΄ λ§λ€ μ μλ dp λ₯Ό λ§λ λ€.
μ 보면 N μ¬μ΄μ¦μ dpλ§μΌλ‘ ν΄κ²° κ°λ₯νλ€.
dp = [0 for _ in range(K+1)]
dp[0] = 1
for i in range(0,N):
for j in range(1,K+1):
if j >= A[i]:
dp[j] = dp[j-A[i]] + dp[j]
print(dp[K])