-
-
Notifications
You must be signed in to change notification settings - Fork 360
[freemjstudio] WEEK 1 Solutions #2674
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
Changes from all commits
865ed8d
1a6de37
9531176
370fe38
db69654
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| class Solution: | ||
| def containsDuplicate(self, nums: List[int]) -> bool: | ||
| first = len(nums) # O(1) : 리스트 객체는 이미 자기자신의 길이를 저장하고 있음 | ||
| set_nums = set(nums) # O(N) | ||
| second = len(set_nums) | ||
| return (first != second) | ||
|
|
||
| # 시간 복잡도 : O(N) |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 빈도 계산은 O(n). 고정 크기 힙을 사용해 상위 k개를 유지하므로 전체 시간은 O(n log k)이고 공간은 O(n) 또는 O(k)로 요약될 수 있습니다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import heapq | ||
|
|
||
| class Solution: | ||
| def topKFrequent(self, nums: List[int], k: int) -> List[int]: | ||
| answer = [] | ||
| count = dict() | ||
| heap = [] | ||
| if len(nums) <= 1: | ||
| return nums | ||
| # O(N) | ||
| for num in nums: | ||
| count[num] = count.get(num, 0) + 1 | ||
|
|
||
| # heap 의 크기는 최대 k 로 제한함. | ||
| # unique 한 M개의 숫자를 선형 순회 O(M) | ||
| for num, freq in count.items(): | ||
| if len(heap) < k: | ||
| heapq.heappush(heap, (freq, num)) # O(logK) | ||
| else: | ||
| if heap[0][0] < freq: | ||
| heapq.heappop(heap) # O(logK) | ||
| heapq.heappush(heap, (freq, num)) # O(logK) | ||
|
|
||
| for _ in range(k): | ||
| k, v = heapq.heappop(heap) | ||
| answer.append(v) | ||
|
|
||
| return answer | ||
|
|
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 목표 합을 이루는 보완 값을 맵에 저장하고, 현재 숫자를 읽으면서 보완 값을 검사합니다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| class Solution: | ||
| def twoSum(self, nums: List[int], target: int) -> List[int]: | ||
| answer = [] | ||
| hashmap = dict() | ||
| for i in range(len(nums)): | ||
| current_num = nums[i] | ||
| diff = target - current_num | ||
| if diff in hashmap.keys(): | ||
| return [i, hashmap[diff]] | ||
| hashmap[current_num] = i # store the index of current num | ||
|
|
||
| return answer |
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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
풀이 1:
Solution.containsDuplicate— Time: O(n) / Space: O(n)피드백: 리스트를 한 번 순회해 모든 원소의 중복 여부를 판별하므로 시간 복잡도는 선형이고, 중복 여부를 저장하기 위한 추가 공간이 필요합니다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 2:
Solution— Time: O(n) / Space: O(n)피드백: 입력 크기에 비례한 추가 해시셋을 사용합니다. 모든 원소를 한 번씩 확인합니다.
개선 제안: 현재 구현이 적절해 보입니다.