DevYoon

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ •์ˆ˜ ์‚ผ๊ฐํ˜• (Python) ๋ณธ๋ฌธ

PS/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ •์ˆ˜ ์‚ผ๊ฐํ˜• (Python)

gimewn 2022. 4. 26. 15:57

link ๐Ÿ”— 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):
    ret = [[0]*len(triangle[i]) for i in range(len(triangle))]
    ret[0][0] = triangle[0][0]
    for y in range(len(ret)-1):
        for x in range(len(triangle[y])):
            ret[y+1][x] = max(ret[y+1][x], triangle[y+1][x]+ret[y][x])
            ret[y+1][x+1] = max(ret[y+1][x+1], triangle[y+1][x+1]+ret[y][x])
    return max(ret[len(triangle)-1])