-
-
Notifications
You must be signed in to change notification settings - Fork 303
[jaejeong1] WEEK 01 solutions #1981
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
| @@ -1,24 +1,35 @@ | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.PriorityQueue; | ||
| import java.util.Queue; | ||
|
|
||
| class SolutionTopKFrequentElements { | ||
| public int[] topKFrequent(int[] nums, int k) { | ||
| // 빈도순으로 k개 반환 | ||
| // 빈도 체크: 해시맵으로 카운트. 시간복잡도 O(N), 공간복잡도 O(N) | ||
| // 빈도순 정렬: sorting, 시간복잡도 O(N log N), 공간복잡도 O(N) | ||
| // 합산: 시간복잡도 O(N log N), 공간복잡도 O(N) | ||
| // 풀이 | ||
| // 시간복잡도: O(N log K), 공간복잡도: O(N) | ||
|
|
||
| // 빈도 체크 | ||
| Map<Integer, Integer> freq = new HashMap<>(); | ||
| for (int num : nums) { | ||
| freq.put(num, freq.getOrDefault(num, 0) + 1); | ||
| } | ||
| // 숫자 별 빈도 누적을 Map 이용 | ||
| // 시간복잡도: O(N), 공간복잡도: O(N) | ||
| Map<Integer, Integer> count = new HashMap<>(); | ||
| for (int n: nums) { | ||
| count.put(n, count.getOrDefault(n, 0) + 1); | ||
| } | ||
|
|
||
| // 빈도순 정렬 | ||
| return freq.keySet().stream() | ||
| .sorted((a, b) -> freq.get(b) - freq.get(a)) | ||
| .mapToInt(i -> i) | ||
| .limit(k) // 배열에서 상위 k개만 반환 | ||
| .toArray(); | ||
| Queue<Integer> heap = new PriorityQueue<>((x, y) -> count.get(x) - count.get(y)); | ||
| // 가장 빈번한 k개의 수를 만들기 위해 우선순위 큐를 사용 | ||
| // 시간복잡도: O(N log k), 공간복잡도: O(N) | ||
| for (int n: count.keySet()) { | ||
| heap.add(n); | ||
| if (heap.size() > k) heap.poll(); // 가장 빈번한 K개가 만족됐으니 더이상 추가하지 않고 제외 | ||
| } | ||
|
|
||
| // k ~ 0 순서대로 힙에서 역순으로 뽑음 | ||
| // 시간복잡도: O(k log k), 공간복잡도: O(N) | ||
| int[] result = new int[k]; | ||
| for(int i = k - 1; i >= 0; --i){ | ||
| result[i] = heap.poll(); | ||
| } | ||
|
|
||
| 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.
안녕하세요.
우선순위큐를 써야지 O(n log(K))가 가능하군요.
작성을 잘해주셔서 새로 배웠네요!
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.
네, 저도 다시 풀어보니 힙(우선순위큐)를 써야 하더라구요 ㅎㅎ 감사합니다!