Skip to content

Commit 997235e

Browse files
committed
[BOJ]#18353_정수삼각형 /Silver1 / 45min(재도전)
https://www.acmicpc.net/problem/1932
1 parent f63e6de commit 997235e

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

Hongjoo/백준/정수삼각형.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""
2+
-top-dowm 방향으로 내려올때 , 각 경로에서 요소들의 최대합을 리스트 dp 에 누적시키기
3+
1, 2중 for 문
4+
2. 모든 경로에 대해 숫자의 누적합을 dp 리스트에 넣기
5+
- 맨 left , right : idx = 0 , -1 경우
6+
=> dp[stage][0] =dp[stage-1][0] + graph[stage][0]
7+
=> dp[stage][-1] =dp[stage-1][-1] + graph[stage][-1]
8+
- 가운데 [idx : 1~ state-1]
9+
=> dp[stage][idx] = max(dp[stage-1][idx] + graph[stage][idx-1] , dp[stage-1][idx] + graph[stage][idx] )
10+
11+
[7]
12+
[10,15]
13+
8 1 0
14+
[18] , [11vs 16 = 16] , [15+0]
15+
2 7 4 4
16+
[18 +2] [18 + 7 vs 16 + 7] [] [15+ 4]
17+
18+
"""
19+
import sys
20+
#1. input
21+
n = int(sys.stdin.readline())
22+
graph= []
23+
for i in range(n) :
24+
tmp = list(map(int, sys.stdin.readline().split()))
25+
graph.append(tmp)
26+
# 2. Initialize List dp
27+
28+
dp = [graph[0]]
29+
30+
#3. dynamic programing
31+
32+
for stage in range(1,n) :
33+
tmp = []
34+
num_list = graph[stage] # add
35+
36+
for idx in range(len(num_list)) :
37+
total_sum = 0
38+
if idx == 0 : # left
39+
total_sum = dp[stage-1][0] + num_list[0]
40+
elif idx < len(num_list)-1: # middle
41+
total_sum = max(dp[stage-1][idx-1] + num_list[idx], dp[stage-1][idx] + num_list[idx])
42+
else: # idx == -1: #right
43+
total_sum = dp[stage-1][-1] + num_list[-1]
44+
tmp.append(total_sum)
45+
46+
dp.append(tmp)
47+
48+
49+
answer = max(dp[-1])
50+
print(f"{answer}")

0 commit comments

Comments
 (0)