-
-
Notifications
You must be signed in to change notification settings - Fork 305
[radiantchoi] WEEK 11 Solutions #2289
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
Open
radiantchoi
wants to merge
3
commits into
DaleStudy:main
Choose a base branch
from
radiantchoi:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+164
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| from typing import List | ||
|
|
||
| # 어떤 그래프가 Valid Tree려면? | ||
| # 순환이 발생하지 않으면서, 모든 노드가 연결되어 있어야 한다. | ||
| # n개의 노드를 모두 연결하는 데 필요한 간선의 갯수는 n - 1개 | ||
| # 순환 발생 탐지 -> Union Find | ||
|
|
||
| class Solution: | ||
| def find(self, parent: List[int], n: int) -> int: | ||
| if parent[n] == n: | ||
| return n | ||
|
|
||
| parent[n] = self.find(parent, parent[n]) | ||
| return parent[n] | ||
|
|
||
| def union(self, parent: List[int], left: int, right: int) -> bool: | ||
| left_parent = self.find(parent, left) | ||
| right_parent = self.find(parent, right) | ||
|
|
||
| if left_parent == right_parent: | ||
| return False | ||
|
|
||
| parent[right_parent] = left_parent | ||
| return True | ||
|
|
||
| def valid_tree(self, n: int, edges: List[List[int]]) -> bool: | ||
| parents = list(range(n)) | ||
|
|
||
| for edge in edges: | ||
| # Union Find에서 False가 반환된다는 것은 순환이 발생한다는 것 | ||
| if not self.union(parents, edge[0], edge[1]): | ||
| return False | ||
|
|
||
| return len(edges) == n - 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| function missingNumber(nums: number[]): number { | ||
| let occurence = new Map<number, boolean>(); | ||
|
|
||
| for (let i = 0; i <= nums.length; i++) { | ||
| occurence.set(i, false); | ||
| } | ||
|
|
||
| for (const num of nums) { | ||
| occurence.set(num, true); | ||
| } | ||
|
|
||
| const results = Array.from(occurence.entries()); | ||
|
|
||
| for (const result of results) { | ||
| if (result[1] === false) { | ||
| return result[0]; | ||
| } | ||
| } | ||
|
|
||
| return 0; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| // Definition for singly-linked list. | ||
| public class ListNode { | ||
| public var val: Int | ||
| public var next: ListNode? | ||
| public init() { self.val = 0; self.next = nil; } | ||
| public init(_ val: Int) { self.val = val; self.next = nil; } | ||
| public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; } | ||
| } | ||
|
|
||
| class Solution { | ||
| // [최초 풀이] | ||
| // - 시간 복잡도: O(N) | ||
| // - 공간 복잡도: O(N) (별도의 리스트 생성) | ||
| // - 전략: 뒤집어진 리스트를 새로 만들고(Deep Copy), 하나씩 꺼내서 기존 리스트의 사이사이에 끼워 넣는다. | ||
| func reorderList(_ head: ListNode?) { | ||
| var rev: ListNode? = nil | ||
| var tracker = head | ||
| var threshold = 0 | ||
|
|
||
| // 뒤집어진 리스트 만들기 (메모리 추가 사용) | ||
| while let point = tracker { | ||
| let temp = rev | ||
| rev = ListNode(point.val) | ||
| rev?.next = temp | ||
| threshold += 1 | ||
|
|
||
| tracker = tracker?.next | ||
| } | ||
|
|
||
| var head = head | ||
| threshold -= 1 | ||
|
|
||
| // 뒤집어진 리스트의 노드 값을 이용해 새로운 노드를 만들어, 사이사이에 삽입 | ||
| while head != nil && threshold > 0 { | ||
| let temp = head?.next | ||
| let newNode = ListNode(rev!.val) | ||
| head?.next = newNode | ||
| head = head?.next | ||
| threshold -= 1 | ||
|
|
||
| guard threshold > 0 else { | ||
| head?.next = nil | ||
| break | ||
| } | ||
|
|
||
| head?.next = temp | ||
| head = head?.next | ||
| rev = rev?.next | ||
| threshold -= 1 | ||
|
|
||
| guard threshold > 0 else { | ||
| head?.next = nil | ||
| break | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // [개선된 풀이] | ||
| // - 시간 복잡도: O(N) | ||
| // - 공간 복잡도: O(1) (In-place 조작) | ||
| // - 전략: Slow & Fast Pointer로 중간을 찾고, 뒷부분만 뒤집은 뒤 병합한다. | ||
| func reorderList2(_ head: ListNode?) { | ||
| // 예외 처리: 노드가 0개, 1개, 2개일 때는 재배열 불필요 | ||
| guard head != nil, head?.next != nil else { return } | ||
|
|
||
| var slow = head | ||
| var fast = head | ||
|
|
||
| // 1. 중간 지점 찾기 (Slow & Fast Runner) | ||
| // fast는 2칸씩 가므로, fast가 끝에 닿으면 slow는 정확히 중간에 위치함. | ||
| // (홀수 개일 땐 정중앙, 짝수 개일 땐 n/2번째 노드) | ||
| while fast?.next != nil && fast?.next?.next != nil { | ||
| slow = slow?.next | ||
| fast = fast?.next?.next | ||
| } | ||
|
|
||
| // 2. 리스트 분리 및 뒷부분 뒤집기 (Reverse Linked List) | ||
| var current = slow?.next | ||
| slow?.next = nil // 앞부분 리스트의 끝을 nil로 막아줌 (완전 분리) | ||
| var rev: ListNode? = nil | ||
|
|
||
| while current != nil { | ||
| let temp = current?.next // Save: 다음 이동할 곳 기억 | ||
| current?.next = rev // Cut & Attach: 방향을 뒤로 돌림 (기존 연결 끊김) | ||
| rev = current // Move: rev(헤드)를 현재로 당겨옴 | ||
| current = temp // Move: 다음 노드로 전진 | ||
| } | ||
|
|
||
| // 3. 두 리스트 병합하기 (Merge) | ||
| var first = head | ||
| var second = rev | ||
|
|
||
| // 리스트 다루기의 대원칙 (Merge 과정) | ||
| // 1. [Save] 노드 뭉치의 next(다음 갈 곳)를 임시 변수에 저장 | ||
| // 2. [Cut & Attach] 노드의 next를 새로운 대상에 연결 | ||
| // (Tip: 굳이 nil로 끊었다가 다시 붙일 필요 없이, 덮어쓰면 기존 연결은 자동으로 끊어짐) | ||
| // 3. [Move] 기준 포인터를 저장해둔 next 위치로 이동 | ||
| while second != nil { | ||
| let firstTemp = first?.next // 1. Save | ||
| let secondTemp = second?.next // 1. Save | ||
|
|
||
| first?.next = second // 2. Cut & Attach (first가 second를 가리킴) | ||
| second?.next = firstTemp // 2. Cut & Attach (second가 원래 first의 다음을 가리킴) | ||
|
|
||
| first = firstTemp // 3. Move | ||
| second = secondTemp // 3. Move | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Map 을 이렇게도 활용할 수 있겠군요! 저는 단순히 set을 사용해서 for문 도는식으로 생각했는데 이 방법이 더 직관적이고 좋은 것 같습니다