728x90
triangle = [[7], [3, 8], [8, 1, 0], [2, 7, 4, 4], [4, 5, 2, 6, 5]]
def solution(triangle):
answer_list = [triangle[0]]
for i in range(1, len(triangle)):
# 해당 층의 길이 확인(합쳐지는 곳 체크)
len_row = len(triangle[i])
temp = []
# 인덱스와 값 확인
for j, v in enumerate(triangle[i]):
if j == 0:
temp.append(answer_list[i-1][0] + v)
print(temp)
elif j == len_row-1:
temp.append(answer_list[i-1][-1] + v)
else:
temp.append(v+max(answer_list[i-1][j-1], answer_list[i-1][j]))
answer_list.append(temp)
return max(answer_list[-1])
solution(triangle)
728x90
'코딩테스트 > Python' 카테고리의 다른 글
프로그래머스 스쿨 - 올바른 괄호(Python)(복습) (0) | 2023.10.21 |
---|---|
프로그래머스 스쿨 - 0떼기(Python) (0) | 2023.10.15 |
프로그래머스 스쿨 - 최솟값 만들기(Python)(복습) (0) | 2023.10.13 |
프로그래머스 스쿨 - 이진 변환 반복하기(Python)(복습) (0) | 2023.10.10 |
프로그래머스 스쿨 - 당구 연습(Python)(복습) (0) | 2023.10.09 |