-
-
Notifications
You must be signed in to change notification settings - Fork 195
[hak.lee] week 1 #305
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
[hak.lee] week 1 #305
Conversation
ref: https://wiki.python.org/moin/TimeComplexity | ||
set는 dict와 거의 비슷하게 구현되어 있는데, | ||
dict의 `Set Item`의 Average Case는 O(1)이더라도 | ||
Amortized Worst Case가 O(n)이다! | ||
|
||
즉, set에 아이템을 추가할때마다 해시 충돌이 일어날 경우 | ||
최악의 경우 O(n^2)이 걸리므로, 아래의 set(nums)의 | ||
TC가 O(n^2)이 되는 것일까..? | ||
|
||
set(nums)의 결과가 최악의 경우 SC가 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.
일반적으로 코딩 테스트에서는 이 정도까지 깊은 복잡도 분석을 요하지는 않습니다. 대부분의 개발자들은 해시 충돌이 거의 일어나지 않는다는 것을 가정하고 해시 테이블 기반의 자료 구조를 사용합니다. 따라서 이 풀이의 시간 복잡도는 O(n)
라고 하시더라도 면접관과 크게 논쟁의 여지는 없을 것 같습니다.
top-k-frequent-elements/haklee.py
Outdated
class Solution: | ||
def topKFrequent(self, nums: List[int], n: int) -> List[int]: | ||
d = {} | ||
[d.update({i: d.get(i, 0) + 1}) for i in nums] |
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.
해당 코드도 충분히 좋아보입니다만, collections 모듈을 써서 가독성을 높이는거도 좋아보입니다 😄
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.
Counter를 써서 한 줄 솔루션으로 바꿔보았습니다!
No description provided.