본문 바로가기
Programming/Programmers

[프로그래머스] 정수삼각형(Python)

by 데이터현 2021. 8. 12.

https://programmers.co.kr/learn/courses/30/lessons/43105

 

코딩테스트 연습 - 정수 삼각형

[[7], [3, 8], [8, 1, 0], [2, 7, 4, 4], [4, 5, 2, 6, 5]] 30

programmers.co.kr

def solution(triangle): 
    for depth in range(1, len(triangle)): 
        for i, v in enumerate(triangle[depth]):
            v1 ,v2 = 0,0
            if(i>0):
                v1 = triangle[depth-1][i-1]
            if(i<len(triangle[depth-1])):
                v2 = triangle[depth-1][i]
            triangle[depth][i] = v + max(v1, v2) 
    return max(triangle[len(triangle) - 1])

댓글