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

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

merged 1 commit into from
Aug 11, 2023

Conversation

ksumini
Copy link
Contributor

@ksumini ksumini commented Aug 9, 2023

PR Summary

풀이시간: 18분

<제한사항>

  • 삼각형의 높이는 1 이상 500 이하
  • 삼각형을 이루고 있는 숫자는 0 이상 9,999 이하의 정수

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

  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])

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

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

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

<추천 문제>

ISSUE NUMBER

@ksumini ksumini self-assigned this Aug 9, 2023
@ksumini ksumini linked an issue Aug 11, 2023 that may be closed by this pull request
Copy link
Contributor

@zsmalla zsmalla left a comment

Choose a reason for hiding this comment

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

제 풀이와 다른 관점에서 dp 풀이를 해주셨기 때문에 재미있게 풀이를 본 것 같습니다!


<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.

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

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.

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

# 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.

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

@@ -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.

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


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

Choose a reason for hiding this comment

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

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

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
Member

Choose a reason for hiding this comment

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

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

# 3) 안쪽에 있는 수들
else:
d[i][j] = max(d[i-1][j-1], d[i-1][j])
d[i][j] += triangle[i][j]
Copy link
Member

Choose a reason for hiding this comment

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

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

@Woo-Yeol Woo-Yeol merged commit aaf8571 into main Aug 11, 2023
@Woo-Yeol Woo-Yeol deleted the sumin-#95 branch August 11, 2023 08:54
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Programmers] 정수 삼각형
3 participants