BAEKJOON : 2156, 1932
No. 2156
1. Problem
2. My Solution
-i 2杯目のワインは飲まない(OOX)
-i号ワイン、i-1ワイン、i-2ワイン(OXO)
-第i種ワインも飲めばi-1ワイン(XOO)
import sys
n = int(sys.stdin.readline().rstrip())
juices = []
for _ in range(n):
juices.append(int(sys.stdin.readline().rstrip()))
dp = [[0,0,0] for _ in range(n)]
dp[0][1] = dp[0][2] = juices[0]
for i in range(1,n):
dp[i][0] = max(dp[i-1])
dp[i][1] = dp[i-1][2] + juices[i]
dp[i][2] = dp[i-1][0] + juices[i]
print(max(dp[n-1]))
3. Others' Solutions 2杯目のワインを飲まないと
-i-1型ワインではなくi-2型ワイン
-i-1ワインも飲めば
- dp[i-1]
- juices[i]+dp[i-2]
-ジュース[i]+ジュース[i-1]+dp[i-3](i-3の最大値を加算)
# dp[i] -> i = n, n 일때 최대 포도주양
import sys
n = int(sys.stdin.readline().rstrip())
juices = [0]
for _ in range(n):
juices.append(int(sys.stdin.readline().rstrip()))
dp = [0] * (n+1)
dp[1] = juices[1]
if n > 1:
dp[2] = juices[1] + juices[2]
for i in range(3,n+1):
temp = []
temp.append(dp[i-1])
temp.append(juices[i]+juices[i-1]+dp[i-3])
temp.append(juices[i]+dp[i-2])
dp[i] = max(temp)
print(dp[n])
4. LearnedNo. 1932
1. Problem
2. My Solution
# dp[i][j] -> i = n, j = 0 ~ level-1 (해당 층에서 j 번째 요소로 끝나는 경로의 최댓값)
import sys
n = int(sys.stdin.readline().rstrip())
dp = [[0]* n for _ in range(n+1)]
level = [[0]]
for _ in range(n):
level.append(list(map(int,sys.stdin.readline().rstrip().split())))
dp[1][0] = level[1][0]
for i in range(2,n+1):
for j in range(0,i):
if j == 0:
dp[i][j] = dp[i-1][j] + level[i][j]
elif j == i-1:
dp[i][j] = dp[i-1][j-1] + level[i][j]
else:
dp[i][j] = max(dp[i-1][j-1] + level[i][j], dp[i-1][j] + level[i][j])
print(max(dp[n]))
Reference
この問題について(BAEKJOON : 2156, 1932), 我々は、より多くの情報をここで見つけました https://velog.io/@codren/BAEKJOON-2156テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol