-
-
Notifications
You must be signed in to change notification settings - Fork 361
[JeonJe] WEEK 02 Solutions #2678
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,41 @@ | ||
| import java.util.*; | ||
|
|
||
| // TC: O(n^2) | ||
| // SC: O(1) | ||
| class Solution { | ||
|
|
||
| public List<List<Integer>> threeSum(int[] nums) { | ||
|
|
||
| List<List<Integer>> answer = new ArrayList<>(); | ||
| int n = nums.length; | ||
| Arrays.sort(nums); | ||
|
|
||
| for (int x = 0; x < n - 2; x++) { | ||
| if (x > 0 && nums[x] == nums[x - 1]) continue; | ||
|
|
||
| int left = x + 1; | ||
| int right = n - 1; | ||
|
|
||
| while (left < right) { | ||
| if (nums[left] + nums[right] + nums[x] == 0) { | ||
| answer.add(Arrays.asList(nums[x], nums[left], nums[right])); | ||
|
|
||
| while (left + 1 < right && nums[left] == nums[left + 1]) { | ||
| left++; | ||
| } | ||
| while (left < right - 1 && nums[right] == nums[right - 1]) { | ||
| right--; | ||
| } | ||
| left++; | ||
| right--; | ||
| } else if (nums[left] + nums[right] + nums[x] < 0) { | ||
| left++; | ||
| } else { | ||
| right--; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| 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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 상수 크기의 memo를 재사용해 중복 계산을 막으나 초기화가 매 호출마다 필요하지 않도록 개선 여지 있음. 개선 제안: 현재 구현이 적절해 보입니다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import java.util.*; | ||
|
|
||
| // TC: O(n) | ||
| // SC: O(n) | ||
| class Solution { | ||
| static int[] memo = new int[46]; | ||
|
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. 문제 조건에 맞춰서 46으로 미리 초기화해두신부분 꼼꼼해서 인상깊습니다! |
||
|
|
||
|
|
||
| public int climbStairs(int n) { | ||
|
|
||
| if (n == 1) return 1; | ||
| if (n == 2) return 2; | ||
| memo[1] = 1; | ||
| memo[2] = 2; | ||
|
|
||
| dfs(n); | ||
| return memo[n]; | ||
| } | ||
|
|
||
| private int dfs(int num) { | ||
|
|
||
| //이미 계산한적이 있으면 | ||
| if (memo[num] != 0) { | ||
| return memo[num]; | ||
| } | ||
|
|
||
| memo[num] = dfs(num - 1) + dfs(num - 2); | ||
| return memo[num]; | ||
|
Comment on lines
+20
to
+28
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. DFS로 문제를 푸셨는데, 언제 DFS로 풀고 언제 bottom-up으로 푸시는지 전제님만의 기준이 궁금합니다! |
||
|
|
||
| } | ||
| } | ||
|
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,22 @@ | ||
| import java.util.*; | ||
|
|
||
| // TC: O(n) | ||
| // SC: 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 right = 1; | ||
| for (int i = n - 2; i >= 0; i--) { | ||
| right = right * nums[i + 1]; | ||
| answer[i] = answer[i] * right; | ||
| } | ||
| 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,21 @@ | ||
| import java.util.*; | ||
|
|
||
| // TC: O(n) | ||
| // SC: O(1) | ||
| class Solution { | ||
| public boolean isAnagram(String s, String t) { | ||
| if (s.length() != t.length()) return false; | ||
|
|
||
| int[] countS = new int[26]; | ||
| int[] countT = new int[26]; | ||
| for (int i = 0; i < s.length(); i++) { | ||
| countS[s.charAt(i) - 'a']++; | ||
| countT[t.charAt(i) - 'a']++; | ||
| } | ||
|
|
||
| for (int i = 0; i < 26; i++) { | ||
| if (countS[i] != countT[i]) return false; | ||
| } | ||
| return true; | ||
| } | ||
| } |
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 부모-자식 간의 흐름을 범위로 추적해 BST 여부를 검증합니다. 개선 제안: 현재 구현이 적절해 보입니다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import java.util.*; | ||
|
|
||
| // TC: O(n) | ||
| // SC: O(h) | ||
| class Solution { | ||
| public boolean isValidBST(TreeNode root) { | ||
| return isValid(root, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY); | ||
|
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. Double 최대, 최솟값 상수를 저렇게 썼군요 배워갑니다! |
||
| } | ||
|
|
||
| private boolean isValid(TreeNode node, double min, double max) { | ||
| if (node == null) { | ||
| return true; | ||
| } | ||
| if (node.val <= min || node.val >= max) { | ||
| return false; | ||
| } | ||
| return isValid(node.left, min, node.val) && isValid(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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 정렬 O(n log n) 이후 두 포인터 반복으로 각 x에 대해 선형 탐색으로 전체 복잡도는 O(n^2)입니다.
개선 제안: 현재 구현이 적절해 보입니다.