-
-
Notifications
You must be signed in to change notification settings - Fork 352
[JeonJe] WEEK 01 Solutions #2646
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.*; | ||
|
|
||
| // TC: O(n) | ||
| // SC: O(n) | ||
| class Solution { | ||
| public boolean containsDuplicate(int[] nums) { | ||
| Set<Integer> set = new HashSet<>(); | ||
| for (int num : nums) { | ||
| if (set.contains(num)) { | ||
| return true; | ||
| } | ||
| set.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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 집합에 원소를 넣고, 각 원소에 대해 연속된 수열의 시작점인지 검사 후 확장하는 방식으로, 모든 원소를 최대 한 번씩만 검사하므로 선형 시간입니다. 개선 제안: 현재 구현이 적절해 보입니다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import java.util.*; | ||
|
|
||
| // TC: O(n) | ||
| // SC: O(n) | ||
| class Solution { | ||
| public int longestConsecutive(int[] nums) { | ||
| if (nums.length == 0 || nums.length == 1) { | ||
| return nums.length; | ||
| } | ||
|
|
||
|
|
||
| HashSet<Integer> set = new HashSet<>(); | ||
| for (int num : nums) { | ||
| set.add(num); | ||
| } | ||
|
|
||
| int answer = 1; | ||
| for (Integer cur : set) { | ||
| if (set.contains(cur - 1)) { | ||
| continue; | ||
| } | ||
|
|
||
| int right = cur; | ||
| while (set.contains(right + 1)) { | ||
| right++; | ||
| } | ||
| answer = Math.max(answer, right - cur + 1); | ||
| } | ||
|
|
||
| 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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 모든 원소의 빈도를 계산한 후, 정렬을 통해 상위 k개를 선택하므로 시간 복잡도는 정렬에 따른 O(n log n)입니다. 정렬을 피하려면 힙 구조를 사용할 수도 있습니다. 개선 제안: 현재 구현이 적절해 보입니다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import java.util.*; | ||
|
|
||
| // TC: O(n log n) | ||
| // SC: O(n) | ||
| class Solution { | ||
| public int[] topKFrequent(int[] nums, int k) { | ||
|
|
||
| Map<Integer, Integer> map = new HashMap<>(); | ||
| for (int num : nums) { | ||
| Integer freq = map.getOrDefault(num, 0); | ||
| map.put(num, freq + 1); | ||
| } | ||
|
|
||
| List<Map.Entry<Integer, Integer>> freqList = new ArrayList<>(map.entrySet()); | ||
|
|
||
| return freqList.stream() | ||
| .sorted(Comparator.comparing(Map.Entry::getValue, Comparator.reverseOrder())) | ||
| .map(Map.Entry::getKey) | ||
| .limit(k) | ||
| .mapToInt(Integer::intValue) | ||
| .toArray(); | ||
|
|
||
| } | ||
| } |
|
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.*; | ||
|
|
||
| // TC: O(n) | ||
| // SC: O(n) | ||
| class Solution { | ||
| public int[] twoSum(int[] nums, int target) { | ||
| Map<Integer, Integer> map = new HashMap<>(); | ||
| for (int i = 0; i < nums.length; i++) { | ||
| if(map.containsKey((target - nums[i])) && map.get(target - nums[i]) != i) { | ||
| return new int[]{i, map.get(target - nums[i])}; | ||
| } | ||
| map.put(nums[i], i); | ||
| } | ||
|
|
||
| return new int[0]; | ||
| } | ||
| } |
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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 집합에 원소를 하나씩 넣으며 이미 존재하는지 검사하므로 시간 복잡도는 원소 수에 비례합니다. 추가로 집합을 저장하는 공간이 필요합니다.
개선 제안: 현재 구현이 적절해 보입니다.