Skip to content

Commit ec6a8da

Browse files
authored
Merge pull request #12 from Mingguriguri/minjeong
Minjeong / 3월 5주차 / 4문제
2 parents b1cbb4e + f77bd40 commit ec6a8da

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
# 입력값 저장
5+
n = int(input())
6+
arr = []
7+
for i in range(n):
8+
temp = list(map(int, input().split()))
9+
arr.append(temp)
10+
11+
# DP 처리
12+
for i in range(1, n):
13+
for j in range(0, i+1):
14+
if j == 0:
15+
arr[i][j] += arr[i-1][j]
16+
elif j == i:
17+
arr[i][j] += arr[i-1][j-1]
18+
else:
19+
arr[i][j] += max(arr[i-1][j], arr[i-1][j-1])
20+
# 최대 합
21+
print(max(arr[n-1]))
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
n = int(input())
5+
cost = []
6+
for i in range(n):
7+
temp = list(map(int, input().split()))
8+
cost.append(temp)
9+
10+
# 0: RED, 1: GREEN, 2: BLUE
11+
for i in range(1, n):
12+
cost[i][0] = min(cost[i-1][1], cost[i-1][2]) + cost[i][0] # RED
13+
cost[i][1] = min(cost[i-1][0], cost[i-1][2]) + cost[i][1] # GREEN
14+
cost[i][2] = min(cost[i-1][0], cost[i-1][1]) + cost[i][2] # BLUE
15+
16+
print(min(cost[-1][0], cost[-1][1], cost[-1][2]))
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
n = int(input())
5+
stair = [0]*301
6+
for i in range(n):
7+
stair[i]=int(input())
8+
9+
DP = [0]*301
10+
DP[0] = stair[0]
11+
DP[1] = stair[0]+stair[1]
12+
DP[2] = max(stair[0]+stair[2], stair[1]+stair[2])
13+
14+
for i in range(3, n):
15+
DP[i] = max(DP[i-3] + stair[i-1] + stair[i], DP[i-2]+stair[i])
16+
17+
print(DP[n-1])
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
n = int(input())
5+
wine = [0] * n
6+
for i in range(n):
7+
wine[i] = int(input())
8+
9+
dp = [0] * n
10+
dp[0] = wine[0]
11+
if n > 1:
12+
dp[1] = wine[0] + wine[1]
13+
14+
# n이 1보다 클 때만 두 번째 포도주부터 로직 실행
15+
for i in range(2, n):
16+
# 현재 포도주를 마시지 않는 경우, 현재 포도주와 이전 포도주를 마시는 경우,
17+
# 그리고 이전 포도주를 마시지 않고 현재 포도주를 마시는 경우를 고려
18+
dp[i] = max(dp[i-1], dp[i-2] + wine[i], dp[i-3] + wine[i-1] + wine[i])
19+
20+
print(dp[-1])

0 commit comments

Comments
 (0)