Skip to content

Commit

Permalink
Create 사칙연산 kyuhyun.py da-in#163
Browse files Browse the repository at this point in the history
  • Loading branch information
lalabulla authored Mar 13, 2023
1 parent 5973d63 commit 17c7659
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions Programmers - 고득점 Kit/[DP] 사칙연산/kyuhyun.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
def solution(arr):
n = len(arr)
dp_max = [[0 for _ in range(n)] for _ in range(n)]
dp_min = [[1001 for _ in range(n)] for _ in range(n)]

# 숫자 담는 배열
num = []
# 기호 담는 배열
log = []

for i in range(len(arr)):
if i % 2 == 0:
num.append(int(arr[i]))
else:
log.append(arr[i])

# 숫자가 1개면 step = 0, 숫자가 두개면 step = 1, ...
for step in range(n):
# step에 따라 만들수 있는 숫자 조합
for i in range(n - step):

j = i + step

if step == 0:
dp_max[i][i] = num[i]
dp_min[i][i] = num[i]
else:
# i 부터 j번째 수까지 괄호 하나씩 늘리면서 계산
for k in range(i, j):
if log[k] == '+' :
dp_max[i][j] = max(dp_max[i][j], dp_max[i][k] + dp_max[k+1][j]) # 더하기의 최대
dp_min[i][j] = min(dp_min[i][j], dp_min[i][k] + dp_min[k+1][j]) # 더하기의 최소
else:
dp_max[i][j] = max(dp_max[i][j], dp_max[i][k] - dp_min[k+1][j]) # 빼기의 최대
dp_min[i][j] = min(dp_min[i][j], dp_min[i][k] - dp_max[k+1][j]) # 빼기의 최소

return dp_max[0][n-1]

0 comments on commit 17c7659

Please sign in to comment.