Skip to content
This repository was archived by the owner on Mar 18, 2024. It is now read-only.
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
28 changes: 28 additions & 0 deletions Programmers/땅따먹기/dohyun.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""

풀이시간
- 약 1시간 풀이 후 실패로 답지 참조

접근법
- N 10만 이하 -> O(NlogN)
- 그리디? -> 현재 행에서 최대라고 해서 합이 최대가 되는게 아니기 때문에 X
- 열은 왜 4개로 고정? -> 각 열별로 결과를 저장해놓으면 될듯
- 현재 열을 제외한 이전 행의 최대값과 현재 열의 값을 계속해서 더해가며 갱신하면 될듯

회고
- 로직은 같은데 1차원 배열로만 해결하려다가 사고가 멈춰버린듯함 ㅠㅠ
- DP테이블은 2차원 배열도 가능하다는거 꼭 인지하기

"""

def solution(land):
N = len(land)

dp = [[0] * 4 for _ in range(N)] # DP 테이블
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

dp 배열을 굳이 만들 필요없이 어차피 아래로 내려가는 구조니까 land배열을 그대로 사용하면 메모리 사용량을 줄일 수 있어요!!

dp[0] = land[0]

for i in range(1, N):
for j in range(4):
dp[i][j] = land[i][j] + max(dp[i-1][:j] + dp[i-1][j+1:]) # 이전 행에서 같은 열을 제외한 나머지 열 중에서 최대 점수 더함
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

아 저도 이렇게 접근을 하다가 land[i][j]를 업데이트 하는 것이 더 좋겠다는 생각을 했었습니다! 고생하셨어요 도현님!


return max(dp[-1]) # 최대 점수 반환