[프로그래머스] 땅따먹기

박형진·2021년 11월 28일
0

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


1. 전체 코드

def solution(land):
    n = len(land)
    for i in range(1, n):
        for j in range(0, len(land[i])):
            if j == 0:
                value = max(land[i-1][1], land[i-1][2], land[i-1][3])
            elif j == 1:
                value = max(land[i-1][0], land[i-1][2], land[i-1][3])
            elif j == 2:
                value = max(land[i-1][0], land[i-1][1], land[i-1][3])
            elif j == 3:
                value = max(land[i-1][0], land[i-1][1], land[i-1][2])
            land[i][j] = land[i][j] + value
    return max(land[-1])

2. 후기

백준에 있는 정수삼각형 문제와 똑같이 접근하면 된다.

중요 포인트는 i+1행이 아니라, i-1행을 탐색해야 한다.

profile
안녕하세요!

0개의 댓글