-
-
Notifications
You must be signed in to change notification settings - Fork 248
[wozlsla] WEEK 02 solutions #1750
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
한 문제를 다양한 풀이 방식으로 정리되어 있어 참고하기에 너무 좋아보입니다 :)
triplets = set() | ||
nums.sort() # O(NlogN), return None | ||
|
||
for i in range(len(nums) - 2): # O(N) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
제가 파이썬 문법을 잘 몰라서 ^^;;
혹시 이 부분은 인덱스가 0부터 nums의 사이즈 -2 까지의 범위인걸까요?_?
혹시 맞다면 범위를 그렇게 잡은 이유가 있을까요~?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
인덱스의 범위가 0
~len(nums)-3
입니다! (파이썬에서 range(a, b)
는 b 미포함)
nums = [-4, -1, -1, 0, 1, 2] 일 때 nums[0] 부터 nums[3] 까지(-4~0)가 순회 대상입니다.
i
그리고 low
, high
두개의 포인터로 총 3개의 수를 보고있기 때문에 유효 범위를 그렇게 설정했습니다!
# Intuition | ||
- | ||
|
||
# Approach |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
접근법별로 시간복잡도, 공간복잡도를 상세히 작성해주신 내용이 너무 도움이 되었습니다 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
어딘지 기억은 안나지만, 이런식으로 사고하는 방식을 키워보라는 글을 봤어요..
전 문제 풀이 이전에 접근 방식이나 사고 방식이 아직 너무 어려워서.. 지푸라기라도 잡는 심정으로🥲 조금씩 생각한 것들을 풀어내는 걸 연습해보려고 해보고 있어요 ㅎ
|
||
for n in range(2, len(dp)): | ||
# current house : nums[n-1] | ||
rob_current = nums[n - 1] + dp[n - 2] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오오.. dp로 풀은게 너무 흥미로워요 👍
그런데 rob_current 이 부분이 사실 조금 이해가 안되서 😅
n 이라는 인덱스가 current 인걸까용? 아님 n-1 이 current 인걸까요..?
그럼 n-1이 current 이라면 dp[n-2]는 current 직전까지의 훔친 값들의 누적일까요..?_?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nums 와 dp의 인덱스를 다르게 가져가고 있습니다! n은 dp의 인덱스이고, nums[n-1]이 현재 털 집입니다.
nums
-> 각 집의 금액, dp
-> 털 수 있는 최대 누적 금액 [0(아무 집도 없을 때), ...]
따라서 nums[n-1]이 현재 집입니다. dp[n-2]는 2칸 전 집까지의 최댓값 입니다.
- n번째 dp는 nums[n-1] (현재 집)을 털지 말지 결정
- rob_current: 현재 집을 털고, n-2까지의 최댓값 더하기
인덱스를 이렇게 가져간 이유는 인덱스 문제 처리를 위한 예외를 두지 않고, 식 하나로 해결하기 위해 구현한 방식입니다!
저번주 문제인데 같이 봐주셨네요!! 그때는 DP로 접근하는 방법으로는 스스로 못 풀어서 이번에 해설을 보며 풀이했습니다.
아직 알고리즘 풀이가 익숙하지 않은 단계라서, 질문에 답변하느라 다시 봤는데 풀어놓고도 생소하네요.. 덕분에 재정리할 수 있었습니다. 감사합니다
답안 제출 문제
작성자 체크 리스트
In Review
로 설정해주세요.검토자 체크 리스트
Important
본인 답안 제출 뿐만 아니라 다른 분 PR 하나 이상을 반드시 검토를 해주셔야 합니다!