본문 바로가기
코딩테스트/Python

프로그래머스 스쿨 - 정수 삼각형(Python)(복습)

by 보안매크로 2023. 10. 14.
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