-
-
Notifications
You must be signed in to change notification settings - Fork 361
[xeulbn] WEEK 02 solutions #2693
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,52 @@ | ||
| import java.util.*; | ||
|
|
||
| //시간복잡도 : O(n^2), 공간복잡도 : O(nlogn) | ||
|
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. 혹시 이거 왜 공간복잡도가 |
||
|
|
||
| class Solution { | ||
| public List<List<Integer>> threeSum(int[] nums) { | ||
|
|
||
| Arrays.sort(nums); | ||
|
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. TMI) js는 기본이 문자열 정렬이라 이 풀이를 그대로 js로 옮기면 틀려요 java 좋네요 [10, 2, 1].sort(); // → [1, 10, 2] 😱 비교자 없으면 문자열 정렬
[10, 2, 1].sort((a,b)=>a-b); // → [1, 2, 10] 이렇게 해야 함 |
||
| List<List<Integer>> answer = new ArrayList<>(); | ||
|
|
||
| for(int i=0;i<nums.length-2;i++){ | ||
| if (i>0 && nums[i] == nums[i-1]) { | ||
| continue; | ||
| } | ||
| int leftIndex = i+1; | ||
| int rightIndex = nums.length-1; | ||
|
|
||
| while(leftIndex<rightIndex){ | ||
| int sum =nums[i]+nums[leftIndex]+nums[rightIndex]; | ||
|
|
||
| if(sum==0){ | ||
| // 정답 추가 | ||
| List<Integer> tmpList = new ArrayList<>(); | ||
| tmpList.add(nums[i]); | ||
| tmpList.add(nums[leftIndex]); | ||
| tmpList.add(nums[rightIndex]); | ||
|
|
||
| answer.add(tmpList); | ||
|
|
||
| // left++, right-- | ||
| leftIndex+=1; | ||
| rightIndex-=1; | ||
|
|
||
| // 중복 제거 | ||
| while(leftIndex<rightIndex && nums[leftIndex]==nums[leftIndex-1]){ | ||
| leftIndex+=1; | ||
| } | ||
| while(leftIndex<rightIndex && nums[rightIndex]==nums[rightIndex+1]){ | ||
| rightIndex-=1; | ||
| } | ||
|
|
||
| }else if(sum<0){ | ||
| leftIndex+=1; | ||
| }else{ | ||
| rightIndex-=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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 정확한 동작과 적절한 초기값 설정으로 선형 시간/공간 복잡도를 만족합니다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import java.util.*; | ||
|
|
||
| //시간복잡도 : O(n), 공간복잡도 : O(n) | ||
| //이전 두 값만 필요하기에, 공간복잡도는 O(1)까지 최적화가 가능. | ||
|
Comment on lines
+3
to
+4
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. 공간 최적화가 잘 되어서 좋은 것 같습니다! |
||
|
|
||
| class Solution { | ||
| public int climbStairs(int n) { | ||
| if(n<=2){ | ||
| return n; | ||
| } | ||
|
|
||
| int[] dp =new int[n+1]; | ||
| dp[1]=1; | ||
| dp[2]=2; | ||
|
|
||
| for(int i=3;i<n+1;i++){ | ||
| dp[i]=dp[i-1]+dp[i-2]; | ||
| } | ||
|
|
||
| return dp[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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
풀이 1:
|
| 복잡도 | |
|---|---|
| Time | O(n) |
| Space | O(n) |
피드백: 해시 세트를 이용한 선형 시간 검사로 최적의 해결
개선 제안: 현재 구현이 적절해 보입니다.
풀이 2: Solution.containsDuplicate — Time: O(n) / Space: O(n)
| 복잡도 | |
|---|---|
| Time | O(n) |
| Space | O(n) |
피드백: 해시 세트를 이용한 선형 시간 검사로 최적의 해결
개선 제안: 현재 구현이 적절해 보입니다.
💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!
| 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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 연속되게 커지는 값을 피하기 위한 최적 부분구조를 올바르게 활용 개선 제안: 현재 구현이 적절해 보입니다.
|
| 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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 각 수를 한 번씩만 탐색하는 방식으로 최적의 시간 복잡도 확보 개선 제안: 현재 구현이 적절해 보입니다.
|
| 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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 두 패스 방식으로 불필요한 나눗셈 없이 구현 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import java.util.*; | ||
|
|
||
| //시간 복잡도 O(n), 공간 복잡도 O(1) | ||
|
|
||
| class Solution { | ||
| public int[] productExceptSelf(int[] nums) { | ||
| int n = nums.length; | ||
| int[] answer = new int[n]; | ||
|
|
||
| answer[0] = 1; | ||
| for (int i=1; i<n; i++) { | ||
| answer[i] = answer[i-1] * nums[i-1]; | ||
| } | ||
|
|
||
| int suffix = 1; | ||
| for (int i = n-1; i >= 0; i--) { | ||
| answer[i] = answer[i] * suffix; | ||
| suffix *= nums[i]; | ||
| } | ||
|
|
||
| return answer; | ||
| } | ||
| } |
| 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; | ||
|
|
||
| } | ||
| } |
| 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[]{}; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import java.util.*; | ||
|
|
||
| // 시간복잡도 : O(n), 공간복잡도 : O(n) | ||
|
|
||
| class Solution { | ||
| public boolean isAnagram(String s, String t) { | ||
|
|
||
| if(s.length()!=t.length()){ | ||
| return false; | ||
| } | ||
|
|
||
| Map<Character,Integer> characterMap = new HashMap<>(); | ||
|
|
||
| for(int i=0;i<s.length();i++){ | ||
| char c = s.charAt(i); | ||
| characterMap.put(c,characterMap.getOrDefault(c,0)+1); | ||
| } | ||
|
|
||
| for(int i=0;i<t.length();i++){ | ||
| char c = t.charAt(i); | ||
|
|
||
| if(!characterMap.containsKey(c)){ | ||
| return false; | ||
| } | ||
|
|
||
| characterMap.put(c, characterMap.get(c) - 1); | ||
| if(characterMap.get(c)<0){ | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import java.util.*; | ||
|
|
||
|
|
||
| public class TreeNode { | ||
| int val; | ||
| TreeNode left; | ||
| TreeNode right; | ||
|
|
||
| TreeNode() { | ||
|
|
||
| } | ||
| TreeNode(int val) { | ||
| this.val = val; | ||
| } | ||
|
|
||
| TreeNode(int val, TreeNode left, TreeNode right) { | ||
| this.val = val; | ||
| this.left = left; | ||
| this.right = right; | ||
| } | ||
| } | ||
|
|
||
| class Solution { | ||
| public boolean isValidBST(TreeNode root) { | ||
| return validate(root, Long.MIN_VALUE, Long.MAX_VALUE); | ||
| } | ||
|
|
||
| private boolean validate(TreeNode node, long min, long max) { | ||
| if (node == null) { | ||
| return true; | ||
| } | ||
|
|
||
| if (node.val <= min || node.val >= max) { | ||
| return false; | ||
| } | ||
|
|
||
| return validate(node.left, min, node.val) && validate(node.right, node.val, max); | ||
| } | ||
| } |
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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
풀이 1:
Solution.threeSum— Time: O(n^2) / Space: O(1)피드백: 정렬과 두 포인터 탐색으로 중복을 제거하고 필요 시 건너뛰기 처리까지 잘 구현되어 있습니다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 2:
Solution.threeSum— Time: O(n^2) / Space: O(1)피드백: 중복 제거 로직이 올바르게 작동하며 시간 복잡도도 최적에 가깝습니다.
개선 제안: 현재 구현이 적절해 보입니다.