Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
📖 풀이한 문제
💡 문제에서 사용된 알고리즘
📜 코드 설명
집이 원형으로 배치 되어 있기 때문에 다음 두 경우로 나눠보자.
a. 첫번째 집을 터는 경우
b. 첫번째 집을 털지 않는 경우
다이나믹 프로그래밍의 풀이순서는 다음과 같다.
테이블 정의
n번째 집을 털었을 때 총 훔친 돈의 최댓값 이 들어갈 배열 정의
초기값 세팅
각 집에 있는 돈을 넣어준다.
a. 첫번째 집을 터는 경우
dp_first[1] = -1;dp_first[2] += dp_first[0];b. 첫번째 집을 털지 않는 경우
dp_second[0] = -1;점화식 선언
인접하지 않은 두 집(i - 2, i - 3번째 집)을 비교해 더 큰 값을 누적해 더한다.
답구하기
맨 마지막 두집을 비교해 dp_first 배열과 dp_second 배열에서 최댓값을 구하고,
그 둘 중에서 최댓값이 answer!
close #62