일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Protocol
- Regular Expression
- Python
- Class
- t1
- 시바견
- 30. Substring with Concatenation of All Words
- Substring with Concatenation of All Words
- Python Code
- 315. Count of Smaller Numbers After Self
- Convert Sorted List to Binary Search Tree
- 운영체제
- 109. Convert Sorted List to Binary Search Tree
- 715. Range Module
- shiba
- Generator
- 43. Multiply Strings
- Decorator
- attribute
- 밴픽
- Python Implementation
- 컴퓨터의 구조
- kaggle
- 프로그래머스
- concurrency
- LeetCode
- 파이썬
- DWG
- data science
- iterator
Archives
- Today
- Total
Scribbling
[Programmers] 사칙연산 본문
https://school.programmers.co.kr/learn/courses/30/lessons/1843?language=cpp
1. Python
def solution(arr):
N = len(arr) // 2 + 1
INF = int(1e9)
max_dp = [[-INF] * N for _ in range(N)]
min_dp = [[INF] * N for _ in range(N)]
for length in range(1, N+1):
for i in range(N-length+1):
if length == 1:
max_dp[i][i] = int(arr[2*i])
min_dp[i][i] = int(arr[2*i])
else:
j = i + length - 1
for k in range(i, j):
opt = arr[2*k+1]
if opt == '+':
max_dp[i][j] = max(max_dp[i][j], max_dp[i][k] + max_dp[k+1][j])
min_dp[i][j] = min(min_dp[i][j], min_dp[i][k] + min_dp[k+1][j])
else:
max_dp[i][j] = max(max_dp[i][j], max_dp[i][k] - min_dp[k+1][j])
min_dp[i][j] = min(min_dp[i][j], min_dp[i][k] - max_dp[k+1][j])
return max_dp[0][-1]
2. C++
#include <vector>
#include <string>
#include <climits>
using namespace std;
int solution(vector<string> arr)
{
int N = arr.size() / 2 + 1;
int max_dp[N][N];
int min_dp[N][N];
for (int i=0; i<N; i++) {
for (int j=0; j<N; j++) {
max_dp[i][j] = -INT_MAX;
min_dp[i][j] = INT_MAX;
}
}
for (int length=1; length<=N; length++) {
for (int i=0; i<N-length+1; i++) {
if (length == 1) {
max_dp[i][i] = stoi(arr[2*i]);
min_dp[i][i] = stoi(arr[2*i]);
}
else {
int j = i + length - 1;
for (int k=i; k<j; k++) {
string opt = arr[2*k+1];
if (opt == "+") {
max_dp[i][j] = max(max_dp[i][j], max_dp[i][k] + max_dp[k+1][j]);
min_dp[i][j] = min(min_dp[i][j], min_dp[i][k] + min_dp[k+1][j]);
} else {
max_dp[i][j] = max(max_dp[i][j], max_dp[i][k] - min_dp[k+1][j]);
min_dp[i][j] = min(min_dp[i][j], min_dp[i][k] - max_dp[k+1][j]);
}
}
}
}
}
return max_dp[0][N-1];
}
'Computer Science > Algorithms & Data Structures' 카테고리의 다른 글
[Programmers] 타겟 넘버 (0) | 2024.06.20 |
---|---|
[Programmers] 도둑질 (0) | 2024.06.18 |
[Programmers] 등굣길 (0) | 2024.06.17 |
[Programmers] 정수 삼각형 (0) | 2024.06.17 |
[Programmers] N으로 표현 (1) | 2024.06.13 |