-
-
Notifications
You must be signed in to change notification settings - Fork 352
[jaekwang97] WEEK 01 Solutions #2645
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
base: main
Are you sure you want to change the base?
Changes from all commits
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,16 @@ | ||
| import java.util.*; | ||
|
|
||
| class Solution { | ||
| public boolean containsDuplicate(int[] nums) { | ||
| Set<Integer> seen = new HashSet<>(); | ||
|
|
||
| for (int num : nums) { | ||
| if (seen.contains(num)) { | ||
| return true; | ||
| } | ||
| seen.add(num); | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } |
|
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 n)입니다. 공간은 빈도수 저장과 큐에 저장하는 데 필요합니다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import java.util.*; | ||
|
|
||
| class Solution { | ||
| static class Item implements Comparable<Item> { | ||
| int num; | ||
| int count; | ||
|
|
||
| Item(int num, int count) { | ||
| this.num = num; | ||
| this.count = count; | ||
| } | ||
|
|
||
| @Override | ||
| public int compareTo(Item other) { | ||
| return Integer.compare(other.count, this.count); | ||
| } | ||
| } | ||
|
|
||
| public int[] topKFrequent(int[] nums, int k) { | ||
| Map<Integer, Integer> counts = new HashMap<>(); | ||
|
|
||
| for (int num : nums) { | ||
| counts.put(num, counts.getOrDefault(num, 0) + 1); | ||
| } | ||
|
|
||
| PriorityQueue<Item> queue = new PriorityQueue<>(); | ||
| for (Map.Entry<Integer, Integer> entry : counts.entrySet()) { | ||
| queue.add(new Item(entry.getKey(), entry.getValue())); | ||
| } | ||
|
|
||
| int[] answer = new int[k]; | ||
| for (int i = 0; i < k; i++) { | ||
| answer[i] = queue.poll().num; | ||
| } | ||
|
|
||
| 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,17 @@ | ||
| import java.util.*; | ||
|
|
||
| class Solution { | ||
| public int[] twoSum(int[] nums, int target) { | ||
| Map<Integer, Integer> seen = new HashMap<>(); | ||
|
|
||
| for (int i = 0; i < nums.length; i++) { | ||
| int complement = target - nums[i]; | ||
| if (seen.containsKey(complement)) { | ||
| return new int[] { seen.get(complement), i }; | ||
| } | ||
| seen.put(nums[i], i); | ||
| } | ||
|
|
||
| return new int[] {}; | ||
| } | ||
| } |
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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 집합에 모든 원소를 하나씩 넣으며 중복 여부를 검사하므로 시간 복잡도는 배열 크기만큼 선형입니다. 추가로 집합이 저장하는 원소 수만큼 공간이 필요합니다.
개선 제안: 현재 구현이 적절해 보입니다.