-
-
Notifications
You must be signed in to change notification settings - Fork 358
[xeulbn] WEEK 01 solutions #2660
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
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,15 @@ | ||
| import java.util.*; | ||
|
|
||
| class Solution { | ||
| public boolean containsDuplicate(int[] nums) { | ||
| Set<Integer> numberCheck = new HashSet<>(); | ||
|
|
||
| for(int num : nums){ | ||
| if(numberCheck.contains(num)){ | ||
| return true; | ||
| } | ||
| numberCheck.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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 한 번의 반복으로 각 위치별 최적값을 계산하며, 배열 크기만큼 반복한다. 공간은 DP 배열 크기와 같다. 개선 제안: 현재 구현이 적절해 보입니다.
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. dp로 구현해주셨네요. 코드 조금 수정하면 공간 복잡도를 더 줄일 수 있을 것 같아요!👍 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import java.util.*; | ||
|
|
||
| class Solution { | ||
|
|
||
| public int rob(int[] nums) { | ||
| if (nums.length == 1) { | ||
| return nums[0]; | ||
| } | ||
|
|
||
| int[] dp = new int[nums.length]; | ||
|
|
||
| dp[0] = nums[0]; | ||
| dp[1] = Math.max(nums[0], nums[1]); | ||
|
|
||
| for (int i = 2; i < nums.length; i++) { | ||
| dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]); | ||
| } | ||
|
|
||
| return dp[nums.length - 1]; | ||
| } | ||
| } |
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 집합에 원소를 넣고, 각 원소에 대해 연속된 수를 확장하는 방식으로, 모든 원소를 최대 한 번씩만 검사한다. 개선 제안: 현재 구현이 적절해 보입니다.
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. hashSet 사용해서 효율적으로 잘 구현해주셨네요. 코드에 시간/공간 복잡도도 적어주시면 좋을 것 같습니다! |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import java.util.*; | ||
|
|
||
| class Solution { | ||
| public int longestConsecutive(int[] nums) { | ||
| Set<Integer> set = new HashSet<>(); | ||
|
|
||
| for (int num : nums) { | ||
| set.add(num); | ||
| } | ||
|
|
||
| int maxLength = 0; | ||
|
|
||
| for (int num : set) { | ||
| if (!set.contains(num - 1)) { | ||
| int currentNum = num; | ||
| int currentLength = 1; | ||
|
|
||
| while (set.contains(currentNum + 1)) { | ||
| currentNum++; | ||
| currentLength++; | ||
| } | ||
|
|
||
| maxLength = Math.max(maxLength, currentLength); | ||
| } | ||
| } | ||
|
|
||
| return maxLength; | ||
| } | ||
| } |
|
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 k)이다. 공간은 맵과 큐를 합쳐 O(n). 개선 제안: 현재 구현이 적절해 보입니다.
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,31 @@ | ||
| import java.util.*; | ||
|
|
||
| class Solution { | ||
| public int[] topKFrequent(int[] nums, int k) { | ||
| Map<Integer,Integer> countMap = new HashMap<>(); | ||
|
|
||
| for(int i=0;i<nums.length;i++){ | ||
| countMap.put(nums[i],countMap.getOrDefault(nums[i],0)+1); | ||
| } | ||
| PriorityQueue<Integer> pq = new PriorityQueue<>( | ||
| (a,b)-> countMap.get(a) - countMap.get(b) | ||
| ); | ||
|
|
||
| for(int num: countMap.keySet()){ | ||
| pq.offer(num); | ||
|
|
||
| if(pq.size()>k){ | ||
| pq.poll(); | ||
| } | ||
| } | ||
|
|
||
| int[] answer = new int[k]; | ||
|
|
||
| for (int i = 0; i < k; i++) { | ||
| answer[i] = pq.poll(); | ||
| } | ||
|
|
||
| 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> numIndexMap = new HashMap<>(); | ||
|
|
||
| for (int i=0; i<nums.length; i++) { | ||
| int need = target - nums[i]; | ||
| if (numIndexMap.containsKey(need)) { | ||
| return new int[]{numIndexMap.get(need), i}; | ||
| } | ||
| numIndexMap.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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 집합에 원소를 하나씩 넣으며 이미 존재하는지 검사하는 방식으로, 시간 복잡도는 원소 개수만큼의 반복으로 O(n)이다. 공간은 최악의 경우 모든 원소를 저장하므로 O(n).
개선 제안: 현재 구현이 적절해 보입니다.