[TIL] Algorithms Day 10 - DP
03/16/23
![[TIL] Algorithms Day 10 - DP](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1679491153461%2F3eb512bb-a04d-4ebb-8236-ce016c27b56e.png&w=3840&q=75)
Problem 1. GCD and LCM
import math
a, b = map(int, input().split())
gcd = math.gcd(a, b)
print(gcd)
print(a * b // gcd)
Problem 2. Triangle Permutation
tri = [0 for i in range(101)]
tri[1] = 1
tri[2] = 1
tri[3] = 1
for i in range(4, 101):
tri[i] = tri[i-3] + tri[i-2]
T = int(input())
for _ in range(T):
N = int(input())
print(tri[N])
Problem 3. RGB Distance
N = int(input())
costs = []
for _ in range(N):
costs.append(list(map(int, input().split())))
for i in range(1, N):
costs[i][0] = min(costs[i-1][1], costs[i-1][2]) + costs[i][0]
costs[i][1] = min(costs[i-1][0], costs[i-1][2]) + costs[i][1]
costs[i][2] = min(costs[i-1][0], costs[i-1][1]) + costs[i][2]
print(min(costs[N-1]))
Problem 4. Integer Triangle
n = int(input())
tri = []
for i in range(n):
tri.append(list(map(int, input().split())))
for i in range(1, n):
for j in range(len(tri[i])):
if j == 0:
tri[i][j] += tri[i-1][j]
elif j == i:
tri[i][j] += tri[i-1][j-1]
else:
tri[i][j] += max(tri[i-1][j-1], tri[i-1][j])
print(max(tri[n-1]))
![[코테] 그리디 문제 - 무지의 먹방 라이브](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1712215455263%2F1ac1f35a-8862-4e42-8d0c-e2bea01e04c0.png&w=3840&q=75)
![[코테] Bfs 토마토](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1709032619170%2F70056896-c857-444b-9c99-45bfcb466806.png&w=3840&q=75)
![[코테] Dfs 문제 유형 - 그래프 내에서 구분하여 카운트 하기](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1709019361383%2Fb0585d72-c808-4169-83a9-2724f312e927.png&w=3840&q=75)
![[코테] DFS vs BFS](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1708971211123%2F71f9386c-6a62-43b2-a602-4d084c24d6cf.png&w=3840&q=75)
![[코테] 여행경로](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1708971251412%2F27ce72ed-8ee7-4d13-a02f-ff4bbe50c4be.png&w=3840&q=75)