-
-
Notifications
You must be signed in to change notification settings - Fork 335
[hyeri0903] WEEK10 Solutions #2585
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
a9afd14
93fb400
7da0598
c6e1bcf
307a0e3
11d656c
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 @@ | ||
| /** | ||
| * 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 invertTree(TreeNode root) { | ||
| /** | ||
| 1.문제: inverted binary tree 출력 | ||
| 2.constraints: node 개수 min = 0, max = 100 | ||
| 3.solution: left, right node swap | ||
| time complexity: | ||
| - BST인 경우 best case : O(log n) | ||
| - skwed 인 경우 worst case: O(n) | ||
| - space complexity: O(h) | ||
| */ | ||
|
|
||
| if(root == null) { | ||
| return null; | ||
| } | ||
|
|
||
| dfs(root); | ||
|
|
||
| return root; | ||
|
|
||
| } | ||
|
|
||
| void dfs(TreeNode root) { | ||
| if(root == null) { | ||
| return; | ||
| } | ||
| //swap | ||
| TreeNode tmp = root.left; | ||
| root.left = root.right; | ||
| root.right = tmp; | ||
|
|
||
| //left recursion | ||
| dfs(root.left); | ||
| //right recursion | ||
| dfs(root.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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 모든 리스트의 노드를 우선순위 큐에 넣고, 최소값을 하나씩 꺼내어 새 리스트에 연결하는 방식입니다. 우선순위 큐 크기는 k이므로 공간 복잡도는 O(k)입니다. 노드 전체를 한 번씩 처리하므로 시간 복잡도는 O(N log k)입니다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /** | ||
| * Definition for singly-linked list. | ||
| * public class ListNode { | ||
| * int val; | ||
|
|
||
| * ListNode next; | ||
| * ListNode() {} | ||
| * ListNode(int val) { this.val = val; } | ||
| * ListNode(int val, ListNode next) { this.val = val; this.next = next; } | ||
| * } | ||
| */ | ||
| class Solution { | ||
| public ListNode mergeKLists(ListNode[] lists) { | ||
| /** | ||
| 1.문제: ascending liknked list 를 모두 sorted list 로 머지해라. | ||
| 2.조건 | ||
| - k = list 길이, 최소 = 0, 최대 = 10^4 | ||
| - 원소값 0 이상, 500 이하 | ||
| 3.풀이 | ||
| - priority queue(min-heap): time = O(n log k), space = O(k) | ||
| - 각 list 를 min heap 에 넣고 하나씩 뽑아서 새 리스트에 연결. | ||
| - Heap 에 들어가고 나올때마다 O(log k), 총 노드 수 N = O(N logk) | ||
| - Heap 사용 -> space = k | ||
| */ | ||
|
|
||
| if(lists == null || lists.length == 0) return null; | ||
|
|
||
| PriorityQueue<ListNode> pq = new PriorityQueue<>(lists.length, (a, b) -> a.val - b.val); | ||
|
|
||
| //all list의 첫 노드 넣기 | ||
| for(ListNode node: lists) { | ||
| if (node != null) { | ||
| pq.offer(node); | ||
| } | ||
| } | ||
|
|
||
| //결과 리스트를 위한 더미 노드 | ||
| ListNode dummy = new ListNode(0); | ||
| ListNode curr = dummy; | ||
|
|
||
| while(!pq.isEmpty()) { | ||
| ListNode node = pq.poll(); //최소값 pop | ||
| curr.next = node; //결과 리스트에 연결 | ||
| curr = curr.next; //포인터 이동 | ||
| //다음 노드를 heap에 insert | ||
| if(node.next != null) { | ||
| pq.offer(node.next); | ||
| } | ||
| } | ||
| return dummy.next; | ||
| } | ||
| } |
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 이진 탐색을 통해 배열이 회전된 상태에서도 정렬된 부분을 찾아내어 target을 효율적으로 찾습니다. 배열의 크기만큼 반복하며, 공간은 상수입니다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| class Solution { | ||
| public int search(int[] nums, int target) { | ||
| /** | ||
| 1.prob: index k기준으로 left rotated 된 array 에서 target index return, 존재하지 않으면 -1 return | ||
| 2.constraints | ||
| - asc 정렬된 배열, 모두 unique 한 값 | ||
| - 반드시 O(log n) 으로 풀 것 | ||
| - num.length min=1, max = 5000 | ||
| 3.solution | ||
| - bruteforce, for문 -> time: O(n) | ||
| - binary search -> time: O(log n), space: O(1) | ||
| */ | ||
|
|
||
| int n = nums.length; | ||
| int left = 0, right = n - 1; | ||
|
|
||
|
|
||
| while(left <= right) { | ||
| int mid = (left + right) / 2; | ||
|
|
||
| //target 찾으면 index return | ||
| if(nums[mid] == target) { | ||
| return mid; | ||
| } | ||
|
|
||
| if(nums[left] <= nums[mid]) { | ||
| //왼쪽이 정렬된 경우 | ||
| if(nums[left] <= target && target < nums[mid]) { | ||
| right = mid - 1; | ||
| } else { | ||
| left = mid + 1; | ||
| } | ||
| } else { | ||
| //오른쪽이 정렬된 경우 | ||
| if(nums[mid] < target && target <= nums[right]) { | ||
| left = mid + 1; | ||
| } else { | ||
| right = mid - 1; | ||
| } | ||
| } | ||
| } | ||
| return -1; | ||
| } | ||
| } |
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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 트리의 모든 노드를 방문하며, 각 노드에서 자식 노드의 위치를 교환하는 방식입니다. 재귀 호출로 인해 호출 스택이 트리의 높이만큼 쌓이므로 공간 복잡도는 높이(h)에 비례합니다.
개선 제안: 현재 구현이 적절해 보입니다.