-
-
Notifications
You must be signed in to change notification settings - Fork 335
[hyeri0903] WEEK 15 Solutions #2635
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,60 @@ | ||
| /** | ||
| * Definition for a binary tree node. | ||
| * 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 TreeNode buildTree(int[] preorder, int[] inorder) { | ||
| /** | ||
| 1.preorder, inorder array를 기반으로 binary tree return | ||
| 2.constraints | ||
| - 모두 unique values | ||
| - length min = 1, max = 3000 | ||
| 3.solutions | ||
| - Root node = preorder[0] | ||
| - inorder 에서 root node 위치 찾기 -> root node 위치 left/right node | ||
| - inorder 에서 구한 left/right node 수로 preorder array 에서 left/right 나눔 | ||
|
|
||
| */ | ||
|
|
||
| return buildTree(preorder, 0, preorder.length - 1, | ||
| inorder, 0, inorder.length - 1); | ||
|
|
||
| } | ||
| private TreeNode buildTree(int[] preorder, int preStart, int preEnd, | ||
| int[] inorder, int inStart, int inEnd) { | ||
| if(preStart > preEnd || inStart > inEnd) return null; | ||
|
|
||
| //preorder 첫번째 값 = root node value | ||
| int rootVal = preorder[preStart]; | ||
| TreeNode root = new TreeNode(rootVal); | ||
|
|
||
| //inorder 에서 root 찾기 | ||
| int rootIndex = 0; | ||
| for(int i = 0; i<inorder.length; i++) { | ||
| if(inorder[i] == rootVal) { | ||
| rootIndex = i; | ||
| break; | ||
| } | ||
| } | ||
| //num of left subtree node | ||
| int leftSize = rootIndex - inStart; | ||
| root.left = buildTree(preorder, preStart+1, preStart + leftSize, | ||
| inorder, inStart, rootIndex - 1); | ||
|
|
||
| root.right = buildTree(preorder, preStart+leftSize+1, preEnd, | ||
| inorder, rootIndex + 1, inEnd); | ||
|
|
||
| return root; | ||
| } | ||
| } |
|
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^2)의 시간 복잡도를 갖습니다. 공간은 상수 공간을 사용하며, 팰린드롬 확장 과정에서 별도 저장 공간이 필요 없습니다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| class Solution { | ||
| public String longestPalindrome(String s) { | ||
| /** | ||
| 1.문제: 가장 긴 팰린드롬 string 찾기. | ||
| 2.조건: | ||
| - s 문자열 길이 최소 1, 최대 1000 | ||
| - 답이 1개가 아닐수도있음. 여러개중 1개만 반환해도 ok | ||
| - s 는 digit, English letters 로 구성 | ||
| 3.풀이 | ||
| - stack 사용 -> TLE 발생 | ||
| - expand around center (각 중심에서 양쪽으로 확장하여 체크) | ||
| - 길이 = 1 이면 바로 return | ||
| Palindrome은 substring 기준이라 전체 문자열 길이와 무관하게 홀수/짝수 케이스가 모두 존재할 수 있기 때문에, 두 경우를 모두 확인해야 합니다. | ||
|
|
||
| Time: O(n²) -> palindrome method 에서 최대 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. 저는 이 문제 풀지 못하고 Editorial을 참고했는데, O(N)으로 풀 수 있는 마나커 알고리즘이 소개되어 있습니다. 흥미 있으시면 이 쪽도 살펴보세요! 개인적으로는 어렵더라구요...
Contributor
Author
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) 풀이 방법도 살펴봐야겠네요. 피드백 감사합니다! |
||
| Space: O(1) | ||
| */ | ||
|
|
||
| int n = s.length(); | ||
| if(n == 1) return s; | ||
| //가운데 문자에서 시작 e.g "babad" n = 5, answer = "b" | ||
| String answer = ""; | ||
|
|
||
| for(int i = 0; i < n; i++) { | ||
| //홀수 e.g "b". 중심이 1개 | ||
| String odd = palindrome(s, i, i); | ||
| //짝수 e.g. "ab". 중심이 2개 | ||
| String even = palindrome(s, i, i + 1); | ||
|
|
||
| String longer = odd.length() > even.length() ? odd : even; | ||
| if(longer.length() > answer.length()) { | ||
| answer = longer; | ||
| } | ||
| } | ||
| return answer; | ||
| } | ||
| String palindrome(String s, int start, int end) { | ||
| while(start >= 0 && end < s.length() && s.charAt(start) == s.charAt(end)) { | ||
| start--; | ||
| end++; | ||
| } | ||
| return s.substring(start+1, end); | ||
| } | ||
| } | ||
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 이 방법은 두 단계(전치와 행 뒤집기)를 통해 in-place로 90도 회전을 수행하며, 시간 복잡도는 행렬의 모든 원소를 한 번씩 방문하므로 O(n^2)입니다. 공간은 추가적인 배열 없이 수행됩니다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| class Solution { | ||
| public void rotate(int[][] matrix) { | ||
| /** | ||
| 1.90도 시계방향으로 회전한 matrix return | ||
| 2.constraints : in-place 로 rotate 할 것 | ||
| 3.solution: 2단계 처리 | ||
| 1) 대각선 기준 뒤집기 transpose, matrix[i][j] => matrix[j][i] | ||
| 2) 각 행 reverse | ||
| time complexity : O(n^2), space: O(1) | ||
| */ | ||
|
|
||
| int n = matrix.length; | ||
|
|
||
| //1.transpose | ||
| for(int i = 0; i < n; i++) { | ||
| for(int j = i+1; j < n; j++) { | ||
| int tmp = matrix[i][j]; | ||
| matrix[i][j] = matrix[j][i]; | ||
| matrix[j][i] = tmp; | ||
| } | ||
| } | ||
|
|
||
| //2.reverse each row | ||
| for(int i = 0; i < n; i++) { | ||
| reverse(matrix[i]); | ||
| } | ||
| } | ||
|
|
||
| void reverse(int[] row) { | ||
| int left = 0; | ||
| int right = row.length - 1; | ||
|
|
||
| while(left < right) { | ||
| int tmp = row[left]; | ||
| row[left] = row[right]; | ||
| row[right] = tmp; | ||
|
|
||
| left++; | ||
| right--; | ||
| } | ||
| } | ||
| } |
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 이 구현은 각 노드마다 isSameTree를 호출하여, 최악의 경우 모든 노드에 대해 서브트리 검사를 수행하므로 시간 복잡도는 O(nm)입니다. 공간은 재귀 호출 스택에 따라 달라집니다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /** | ||
| * Definition for a binary tree node. | ||
| * 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 isSubtree(TreeNode root, TreeNode subRoot) { | ||
| /** | ||
| 1.root 트리의 subtree가 subRoot tree 이면 true 아니면 false return | ||
| 2.constraints | ||
| - subRoot tree 가 존재하는데 거기에 자식 노드가 추가로 있으면 안됨 | ||
| - 자기 자신 root tree = subRoot tree 일수도 있음 | ||
| 3.solution | ||
| - 재귀함수로 subTree 찾고 subRoot 의 트리와 동일한지 검사 | ||
| - root 트리 노드 수 : n, subRoot 트리 노드 수 : m | ||
| - timeComplexity : O(nm) | ||
| */ | ||
| if(root == null) return false; | ||
|
|
||
| //먼저 트리가 동일한지 체크 -> left subTree check -> right subTree check | ||
| return isSameTree(root, subRoot) | ||
| || isSubtree(root.left, subRoot) | ||
| || isSubtree(root.right, subRoot); | ||
|
|
||
| } | ||
| private boolean isSameTree(TreeNode a, TreeNode b) { | ||
| //둘 다 null | ||
| if (a == null && b == null) return true; | ||
| //둘중 하나만 null | ||
| if (a == null || b == null) return false; | ||
| //값이 다른 경우 | ||
| if(a.val != b.val) return false; | ||
| //현재 값이 다르면 왼쪽끼리, 오른쪽끼리 비교, 모두 같아야 true return | ||
| return isSameTree(a.left, b.left) && isSameTree(a.right, b.right); | ||
| } | ||
| } |
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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 현재 구현은 inorder 배열에서 루트 노드 위치를 찾기 위해 선형 탐색을 수행하여, 최악의 경우 전체 배열 크기만큼 반복하므로 시간 복잡도가 O(n^2)입니다. 이를 개선하려면 inorder 값의 인덱스를 미리 맵에 저장하여 O(1) 탐색으로 변경하는 방법이 있습니다.
개선 제안: 고려해볼 만한 대안: inorder 값의 인덱스를 미리 해시맵에 저장하여 탐색 시간을 O(1)로 줄이면 전체 시간 복잡도를 O(n)으로 개선할 수 있습니다.