-
-
Notifications
You must be signed in to change notification settings - Fork 245
[hypoxisaurea] Week 1 Solutions #1722
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ed7e3aa
Solve two-sum
hypoxisaurea 990065d
Add solutions
hypoxisaurea 1c2b478
Add solutions
hypoxisaurea de3b81b
Add Solutions
hypoxisaurea 6e5f6c5
Solve Week1
hypoxisaurea d0c6382
Merge branch 'DaleStudy:main' into main
hypoxisaurea File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
''' | ||
Bucket Sort 접근: | ||
1. Counter로 각 숫자의 빈도를 계산한다. | ||
2. 빈도 수를 인덱스로 하는 버킷(buckets)을 생성한다. | ||
- buckets[i]에는 등장 횟수가 i번인 숫자들이 들어간다. | ||
3. 버킷의 가장 높은 빈도 인덱스부터 역순으로 순회하며 | ||
숫자를 하나씩 결과 리스트(result)에 추가한다. | ||
4. 결과 리스트에 k개가 모이면 반환한다. | ||
|
||
시간 복잡도: O(n) | ||
- 빈도 계산 O(n) | ||
- 버킷 채우기 O(n) | ||
- 결과 수집 O(n) | ||
|
||
공간 복잡도: O(n) | ||
- Counter 딕셔너리와 버킷 리스트 사용 | ||
''' | ||
|
||
|
||
from typing import List | ||
from collections import Counter | ||
|
||
|
||
class Solution: | ||
def topKFrequent(self, nums: List[int], k: int) -> List[int]: | ||
freq = Counter(nums) | ||
|
||
buckets = [[] for _ in range(len(nums) + 1)] | ||
for num, count in freq.items(): | ||
buckets[count].append(num) | ||
|
||
result = [] | ||
for i in range(len(buckets) - 1, 0, -1): | ||
for num in buckets[i]: | ||
result.append(num) | ||
|
||
if len(result) == k: | ||
return result | ||
|
||
return result | ||
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.
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.
세개의 모든 파일 린트에러로 VS 사용시는 'VSCode 설정 : "files.insertFinalNewline": true" 으로 해결 혹은 각 파일에 한줄씩 빈 줄을 추가하여 에러 해결하고 머지 해주시길 바랍니다.