-
-
Notifications
You must be signed in to change notification settings - Fork 195
[ayosecu] WEEK 02 solutions #1206
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
""" | ||
- Algorithm | ||
- Sort and compares with three pointers: target, left(l), right(r) | ||
- Time Complexity: O(n^2), n = len(nums) | ||
- sort : O(nlogn) | ||
- nested two loops : O(n^2) | ||
- O(nlogn + n^2) => O(n^2) | ||
- Space Complexity: O(n^2) if result included. | ||
- result size : result.append() called in n^2 times (nested two loops) | ||
""" |
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.
@ayosecu 님 안녕하세요! 저도 투포인터를 활용하여 문제를 풀었습니다. 개인적으로 알고리즘 문제를 풀 때 tc, sc를 생각해내는 게 항상 어려운데, 이 부분을 잘 정리해주셔서 복습하는 데 도움이 많이 됐습니다!
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.
이 문제는 반복문으로도 풀어보시면 좋을 것 같습니다! 그리고 항상 테스트 케이스를 기록하시는 모습이 인상 깊었고, 그 부분에서 많이 배웠습니다!
def productExceptSelfN(self, nums: List[int]) -> List[int]: | ||
n = len(nums) | ||
prefix, suffix, result = [0] * n, [0] * n, [0] * n | ||
|
||
# Calculate prefix and suffix production | ||
prefix[0], suffix[-1] = nums[0], nums[-1] | ||
for i in range(1, n - 1): | ||
prefix[i] = prefix[i - 1] * nums[i] | ||
j = n - i - 1 | ||
suffix[j] = suffix[j + 1] * nums[j] | ||
|
||
# Update the result | ||
result[0], result[-1] = suffix[1], prefix[-2] | ||
for i in range(1, n - 1): | ||
result[i] = prefix[i - 1] * suffix[i + 1] | ||
|
||
return result |
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.
저는 두 번째 풀이 방식으로 문제를 풀었는데, 이 풀이는 제가 생각하지 못했던 방법이라 보면서 공부가 되었습니다! 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.
리뷰 너무 감사합니다! 이번주도 고생하셨고, 다음주도 화이팅입니다! 😄
답안 제출 문제
작성자 체크 리스트
In Review
로 설정해주세요.검토자 체크 리스트
Important
본인 답안 제출 뿐만 아니라 다른 분 PR 하나 이상을 반드시 검토를 해주셔야 합니다!