Skip to content
This repository was archived by the owner on Mar 18, 2024. It is now read-only.

[2023-08-11] sumin #95 #106

Merged
merged 1 commit into from
Aug 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions Programmers - 문제풀이/정수 삼각형/sumin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
"""
풀이시간: 18분
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저는 모든 문제마다 그러는데 수민님 풀이시간에 항상 입이 떡 벌어지고 리뷰를 시작합니다 ㅎㅎ


<제한사항>
- 삼각형의 높이는 1 이상 500 이하
- 삼각형을 이루고 있는 숫자는 0 이상 9,999 이하의 정수

<solution>
DP의 가장 기본적인 유형
그리디로 풀 수 없는 이유는 매 번 가장 큰 수를 선택하는 것이 가장 큰 합을 만들 수 없기 때문!
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저는 오히려 현재 i+1 번째 층의 입장에서 받을 수 있는 i번째 층의 두 수 중 항상 큰걸 가져가는 방법으로 구현했기 때문에 그리디와도 닮아있는 dp문제라고 생각했습니다!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 그것도 좋은 생각합니다. 하지만 저는 그리디는 그 순간의 선택이 최선의 선택이라는 것에 집중되어야한다고 생각했을 때 지금의 선택이 최선이 아니기 때문에 그리디는 아니라고 판단했습니다. 그 근거로 마지막까지 해당 높이의 합산된 값을 가져가야하는 것을 보며 그리디는 아니라고 보았습니다.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

맞네요 계속 현재 입장에서 큰 걸 선택하더라도 마지막 층에서 결과가 다른 경우가 더 클 수 있으니까요! 좋습니다!


1. 테이블 정의하기
D[i][j]: i번째 층의 j번째 위치에 올 수 있는 가장 큰 수

2. 점화식 찾기
1) 가장 왼쪽에 있는 수(j == 0)
- d[i][j] = d[i-1][j]
2) 가장 오른쪽에 있는 수 (j == i)
- d[i][j] = d[i-1][j-1]
3) 안쪽에 있는 수들(1 <= j <= i-1)
- d[i][j] = max(d[i-1][j-1], d[i-1][j])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Woo-Yeol 제가 말한 이상적인 점화식 형태입니다.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

완벽한 정석이네요 감사합니다!


d[i][j] += triangle[i][j]

3. 초기값 정하기
d[0][0] = triangle[0][0]

<시간복잡도>
O(n^2)
- n은 최대 500이기 때문에 500 * 500 = 2,500으로 충분히 통과가능
"""


from typing import List

def solution(triangle: List) -> int:
n = len(triangle)

# 1. 테이블 정의
d = [[0] * (level+1) for level in range(n)]
# 2. 초기값 정하기
d[0][0] = triangle[0][0]

# 3. 점화식
for i in range(1, n):
for j in range(i+1):
# 1) 가장 왼쪽에 있는 수
if j == 0:
d[i][j] = d[i-1][j]
# 2) 가장 오른쪽에 있는 수
elif j == i:
d[i][j] = d[i-1][j-1]
# 3) 안쪽에 있는 수들
else:
d[i][j] = max(d[i-1][j-1], d[i-1][j])
d[i][j] += triangle[i][j]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

d 테이블이 결과적으로 더해줄 원소들을 처음에 가지고 있고, 거기에 triangle에서 기존 정의된 수를 더해주는 방식이군요! 저와는 관점이 바뀐 풀이인 것 같습니다. 흥미롭네요!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

직관적인 점화식을 코드로 옮겨주셔서 너무 보기 편했습니다 수민님 감사합니다.


answer = max(d[n-1])
return answer


triangle = [[7], [3, 8], [8, 1, 0], [2, 7, 4, 4], [4, 5, 2, 6, 5]]
print(solution(triangle))


"""
정확성 테스트
테스트 1 〉 통과 (0.02ms, 10.2MB)
테스트 2 〉 통과 (0.03ms, 10.2MB)
테스트 3 〉 통과 (0.06ms, 10.2MB)
테스트 4 〉 통과 (0.20ms, 10.3MB)
테스트 5 〉 통과 (1.48ms, 10.4MB)
테스트 6 〉 통과 (0.45ms, 10.4MB)
테스트 7 〉 통과 (1.46ms, 10.5MB)
테스트 8 〉 통과 (0.32ms, 10.1MB)
테스트 9 〉 통과 (0.02ms, 10.4MB)
테스트 10 〉 통과 (0.19ms, 10.4MB)

효율성 테스트
테스트 1 〉 통과 (45.10ms, 17.9MB)
테스트 2 〉 통과 (34.41ms, 16.1MB)
테스트 3 〉 통과 (55.93ms, 18.8MB)
테스트 4 〉 통과 (44.57ms, 18MB)
테스트 5 〉 통과 (41.51ms, 17.2MB)
테스트 6 〉 통과 (52.64ms, 19.2MB)
테스트 7 〉 통과 (48.32ms, 18.6MB)
테스트 8 〉 통과 (41.16ms, 16.9MB)
테스트 9 〉 통과 (46.55ms, 17.4MB)
테스트 10 〉 통과 (56.48ms, 18.6MB)
"""