Skip to content

Commit 78d6982

Browse files
committed
[#15] Feat: Add 행렬 곱셈 순서
1 parent f8f782d commit 78d6982

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

11049.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# 행렬 곱셈 순서
2+
3+
N = int(input())
4+
5+
R = [0]*N
6+
C = [0]*N
7+
8+
for n in range(N):
9+
r, c = map(int, input().split())
10+
R[n] = r
11+
C[n] = c
12+
13+
14+
# def solution(X, Y):
15+
# if Y-X <= 0:
16+
# return 0
17+
# minimum = 1e9
18+
# for k in range(X, Y):
19+
# minimum = min(minimum, solution(X, k)+solution(k+1, Y)+R[X]*C[k]*C[Y])
20+
# return minimum
21+
22+
# dp 추가
23+
dp = [[0]*N for _ in range(N)]
24+
25+
26+
def solution(X, Y):
27+
if dp[X][Y] > 0:
28+
return dp[X][Y]
29+
if Y-X <= 0:
30+
return 0
31+
dp[X][Y] = float('inf')
32+
for k in range(X, Y):
33+
dp[X][Y] = min(dp[X][Y], solution(X, k) +
34+
solution(k+1, Y)+R[X]*C[k]*C[Y])
35+
return dp[X][Y]
36+
37+
38+
print(solution(0, N-1))

0 commit comments

Comments
 (0)