반응형
링크 : https://programmers.co.kr/learn/courses/30/lessons/42897
def dp(visited, money, n):
answer = 0
for i in range(n, len(money)):
visited[i] = max(visited[i-1], visited[i-2]+money[i])
#첫번째 집 간 경우, 마지막 집도 갔다면 빼주어야함
if visited[0] != 0 :
if visited[-1] == visited[-2]:
return visited[-1]
else:
return visited[-2]
else:
return visited[-1]
def solution(money):
answer = 0
visited = [0 for x in range(len(money))]
answer = max(dp([money[0], money[0], money[0]+money[2] ]+visited[3:], money, 3), dp([0, money[1], money[1] ]+visited[3:], money, 3), dp([0, 0, money[2] ]+visited[3:], money, 3))
return answer
반응형
'알고리즘 | Algorithm' 카테고리의 다른 글
[LeetCode 리트코드] Find a Corresponding Node of a Binary Tree in a Clone of That Tree (0) | 2021.01.07 |
---|---|
[LeetCode 리트코드] Kth Missing Positive Number (0) | 2021.01.07 |
[LeetCode 리트코드] Remove Duplicates from Sorted List IISolution - Python (0) | 2021.01.05 |
[프로그래머스] 정수삼각형 (DP, 파이썬) (0) | 2020.05.05 |
Firebase Login error 오류 해결 (0) | 2020.01.03 |