Repository navigation
[heesun-task] WEEK 01 Solutions #2922
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
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,18 @@ | ||
| class Solution { | ||
| public boolean containsDuplicate(int[] nums) { | ||
| // Create a set to track seen numbers | ||
| Set<Integer> seen = new HashSet<>(); | ||
|
|
||
| // Loop through the array and return true if a duplicate is found | ||
| for (int num : nums) { | ||
| if (!seen.add(num)) { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
|
|
||
| // Time Complexity: O(n) on average | ||
| // Space Complexity: O(n) | ||
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 @@ | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.PriorityQueue; | ||
|
|
||
| class Solution { | ||
| public int[] topKFrequent(int[] nums, int k) { | ||
| // Count each number's frequency using a hash map. | ||
| Map<Integer, Integer> freq = new HashMap<>(); | ||
| for (int num : nums) { | ||
| freq.put(num, freq.getOrDefault(num, 0) + 1); | ||
| } | ||
|
|
||
| // Keep the top k numbers in a min-heap ordered by frequency. | ||
| PriorityQueue<Integer> heap = new PriorityQueue<>( | ||
| (a, b) -> Integer.compare(freq.get(a), freq.get(b)) | ||
|
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.
|
||
| ); | ||
|
|
||
| for (int num : freq.keySet()) { | ||
| if (heap.size() < k) { | ||
| heap.offer(num); | ||
| } else if (freq.get(num) > freq.get(heap.peek())) { | ||
| // Replace the least frequent number. | ||
| heap.poll(); | ||
| heap.offer(num); | ||
| } | ||
| } | ||
|
|
||
| // Return the numbers in the heap. | ||
| int[] result = new int[k]; | ||
| for (int i = 0; i < k; i++) { | ||
| result[i] = heap.poll(); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
| } | ||
|
|
||
| // Time Complexity: O(n + u log k) | ||
| // Space Complexity: O(u + k) | ||
|
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. k값은 u보다 클 수 없기 때문에 O(u) 로 표기해도 될 것 같습니다. |
||
| // n = nums.length, u = number of unique numbers | ||
Oops, something went wrong.
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.
Set의 add 함수가 boolean 값을 반환한다는 점을 이용하여 푼 점이 인상 깊었습니다.