[프로그래머스] 정수삼각형 파이썬 풀이
[문제 링크]
https://programmers.co.kr/learn/courses/30/lessons/43105
[입출력 예]
triangle | result |
---|---|
[[7], [3, 8], [8, 1, 0], [2, 7, 4, 4], [4, 5, 2, 6, 5]] | 30 |
def solution(triangle): answer = [] #1 #answer의 i번째 원소에는 i번째 줄 까지 왔을 때 각 위치에서 누적값의 최대값 for line_index, line in enumerate(triangle): answer.append([]) for element_index,element in enumerate(line): if line_index ==0: answer[0].append(element) #2 # 처음 원소는 그냥 추가 if line_index >0 \ and (element_index == 0 or element_index == len(line)-1): #3 # 한 줄 안에서 맨 왼쪽이거나 맨 오른쪽일 경우 # 값을 물려받을 경로가 한 군데 밖에 없음. answer[line_index].append(element+answer[line_index-1][0] \ if element_index == 0 \ else element+answer[line_index-1][element_index-1]) else: answer[line_index].append(element+max(answer[line_index-1][element_index],answer[line_index-1][element_index-1])) #4 # 가운데 있는 element일 경우 왼쪽 부모와 오른쪽 부모 중 높은 값과 자신을 합함. return max(answer[-1])
'PS > Programmers' 카테고리의 다른 글
[프로그래머스] 순위 (0) | 2021.11.27 |
---|---|
[프로그래머스] 네트워크 파이썬 (0) | 2021.11.27 |
[프로그래머스] 디스크 컨트롤러 파이썬 (0) | 2021.11.25 |
[프로그래머스] 가장 먼 노드 파이썬 풀이 (0) | 2021.11.25 |
[프로그래머스] N으로 표현 파이썬 풀이 (0) | 2021.11.25 |
댓글
이 글 공유하기
다른 글
-
[프로그래머스] 순위
[프로그래머스] 순위
2021.11.27 -
[프로그래머스] 네트워크 파이썬
[프로그래머스] 네트워크 파이썬
2021.11.27 -
[프로그래머스] 디스크 컨트롤러 파이썬
[프로그래머스] 디스크 컨트롤러 파이썬
2021.11.25 -
[프로그래머스] 가장 먼 노드 파이썬 풀이
[프로그래머스] 가장 먼 노드 파이썬 풀이
2021.11.25
댓글을 사용할 수 없습니다.