STUDY/Algorithm

[프로그래머스] LEVEL3 정수삼각형, python

sinawi95 2021. 4. 22. 20:58
728x90

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):
    dp = [[0] * i for i in range(1, len(triangle) + 1)]
    dp[0][0] = triangle[0][0]
    for i in range(1, len(dp)):
        for j in range(len(dp[i])):
            if j == 0:
                dp[i][j] = triangle[i][j] + dp[i - 1][j]
            elif j == len(dp[i]) - 1:
                dp[i][j] = triangle[i][j] + dp[i - 1][j - 1]
            else:
                dp[i][j] = triangle[i][j] + max(dp[i - 1][j],dp[i - 1][j - 1])
    return max(dp[len(triangle)-1])

동적계획법

쉽게 풀었다.