티스토리 뷰
728x90
반응형
문제
https://programmers.co.kr/learn/courses/30/lessons/12913?language=python3
코딩테스트 연습 - 땅따먹기
땅따먹기 게임을 하려고 합니다. 땅따먹기 게임의 땅(land)은 총 N행 4열로 이루어져 있고, 모든 칸에는 점수가 쓰여 있습니다. 1행부터 땅을 밟으며 한 행씩 내려올 때, 각 행의 4칸 중 한 칸만 밟
programmers.co.kr
코드
def solution(land):
answer = 0
for i in range(1,len(land)):
land[i][0] += max(land[i-1][1], land[i-1][2], land[i-1][3])
land[i][1] += max(land[i-1][0], land[i-1][2], land[i-1][3])
land[i][2] += max(land[i-1][0], land[i-1][1], land[i-1][3])
land[i][3] += max(land[i-1][0], land[i-1][1], land[i-1][2])
return max(land[-1])
DP를 이용하는 문제이다.
맨 위에 행은 냅둬도 되어서 1부터 시작한다.
자신의 열은 제외하고 가장 큰 값을 구한다.
다른 방식 코드
def solution(land):
for i in range(1,len(land)):
for j in range(len(land[0])):
land[i][j] += max(land[i-1][:j] + land[i-1][j+1:])
return max(land[len(land)-1])
자신의 열을 제외하기 위해서 슬라이싱을 사용하여 자신의 열만 제외한다.
반응형
'알고리즘 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] Level2, 가장 큰 정사각형 찾기 (Python) (0) | 2021.06.08 |
---|---|
[프로그래머스] Level2, 올바른 괄호 (Python) (0) | 2021.06.08 |
[프로그래머스] Level2, 최솟값 만들기 (Python) (0) | 2021.06.08 |
[프로그래머스] Level2, 숫자의 표현 (Python) (0) | 2021.06.08 |
[프로그래머스] Level2, 행렬의 곱셈 (Python) (0) | 2021.06.07 |
댓글