-
-
Notifications
You must be signed in to change notification settings - Fork 263
[SeongA] Week 13 Solutions #1946
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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,139 @@ | ||
| class MedianFinder { | ||
|
|
||
| struct Heap { | ||
| private var element: [Int] = [] | ||
| private let isMinHeap: Bool | ||
| var count: Int { | ||
| return element.count | ||
| } | ||
|
|
||
| init(isMinHeap: Bool) { | ||
| self.isMinHeap = isMinHeap | ||
| } | ||
|
|
||
| func peek() -> Int? { | ||
| return element.first | ||
| } | ||
|
|
||
| mutating func insert(_ value: Int) { | ||
| element.append(value) | ||
|
|
||
| var currentIndex = element.count - 1 | ||
|
|
||
| while currentIndex > 0 { | ||
| let parentIndex = (currentIndex - 1) / 2 | ||
|
|
||
| if isMinHeap { | ||
| if element[currentIndex] < element[parentIndex] { | ||
| let temp = element[currentIndex] | ||
| element[currentIndex] = element[parentIndex] | ||
| element[parentIndex] = temp | ||
|
|
||
| currentIndex = parentIndex | ||
| } else { | ||
| break | ||
| } | ||
| } else { | ||
| if element[currentIndex] > element[parentIndex] { | ||
| let temp = element[currentIndex] | ||
| element[currentIndex] = element[parentIndex] | ||
| element[parentIndex] = temp | ||
|
|
||
| currentIndex = parentIndex | ||
| } else { | ||
| break | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| mutating func remove() -> Int? { | ||
| guard !element.isEmpty else { return nil } | ||
|
|
||
| if element.count == 1 { | ||
| return element.removeLast() | ||
| } | ||
|
|
||
| let value = element[0] | ||
|
|
||
| element[0] = element.removeLast() | ||
| siftDown(index: 0) | ||
|
|
||
| return value | ||
| } | ||
|
|
||
| mutating private func siftDown(index: Int) { | ||
| var parentIndex = index | ||
| var count = element.count | ||
|
|
||
| while parentIndex < count { | ||
| let leftChildIndex = parentIndex * 2 + 1 | ||
| let rightChildIndex = parentIndex * 2 + 2 | ||
| var tempIndex = parentIndex | ||
|
|
||
| if isMinHeap { | ||
| if leftChildIndex < count && element[leftChildIndex] < element[parentIndex] { | ||
| tempIndex = leftChildIndex | ||
| } | ||
|
|
||
| if rightChildIndex < count && element[rightChildIndex] < element[tempIndex] { | ||
| tempIndex = rightChildIndex | ||
| } | ||
|
|
||
| } else { | ||
| if leftChildIndex < count && element[leftChildIndex] > element[parentIndex] { | ||
| tempIndex = leftChildIndex | ||
| } | ||
|
|
||
| if rightChildIndex < count && element[rightChildIndex] > element[tempIndex] { | ||
| tempIndex = rightChildIndex | ||
| } | ||
| } | ||
|
|
||
| if parentIndex == tempIndex { | ||
| break | ||
| } | ||
|
|
||
| element.swapAt(parentIndex, tempIndex) | ||
| parentIndex = tempIndex | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private var maxHeap = Heap(isMinHeap: false) | ||
| private var minHeap = Heap(isMinHeap: true) | ||
|
|
||
| init() { | ||
|
|
||
| } | ||
|
|
||
| func addNum(_ num: Int) { | ||
| if maxHeap.count == 0 { | ||
| maxHeap.insert(num) | ||
| return | ||
| } | ||
|
|
||
| if num <= maxHeap.peek()! { | ||
| maxHeap.insert(num) | ||
| } else { | ||
| minHeap.insert(num) | ||
| } | ||
|
|
||
| if minHeap.count > maxHeap.count { | ||
| maxHeap.insert(minHeap.remove()!) | ||
| } | ||
|
|
||
| if maxHeap.count > minHeap.count + 1 { | ||
| minHeap.insert(maxHeap.remove()!) | ||
| } | ||
| } | ||
|
|
||
| func findMedian() -> Double { | ||
| if maxHeap.count == minHeap.count { | ||
| return Double((maxHeap.peek()! + minHeap.peek()!)) / 2 | ||
| } | ||
|
|
||
| return Double(maxHeap.peek()!) | ||
| } | ||
| } | ||
|
|
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,31 @@ | ||
| class Solution { | ||
| // Time O(k) | ||
| // Space O(height of Tree) | ||
| func kthSmallest(_ root: TreeNode?, _ k: Int) -> Int { | ||
| var count = 0 | ||
| var result = root!.val | ||
|
|
||
| inorderTree(root, &count, k, &result) | ||
|
|
||
| return result | ||
| } | ||
|
|
||
| private func inorderTree(_ node: TreeNode?, _ count: inout Int, _ k: Int, _ result: inout Int) { | ||
| guard let node = node else { return } | ||
| if count == k { return } | ||
|
|
||
| // left search | ||
| inorderTree(node.left, &count, k, &result) | ||
|
|
||
| // current node | ||
| count += 1 | ||
| if count == k { | ||
| result = node.val | ||
| return | ||
| } | ||
|
|
||
| // right search | ||
| inorderTree(node.right, &count, k, &result) | ||
| } | ||
| } | ||
|
|
||
22 changes: 22 additions & 0 deletions
22
lowest-common-ancestor-of-a-binary-search-tree/delight010.swift
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,22 @@ | ||
| class Solution { | ||
| // Time O(height) | ||
| // Space O(height) | ||
| func lowestCommonAncestor(_ root: TreeNode?, _ p: TreeNode?, _ q: TreeNode?) -> TreeNode? { | ||
| var current = root | ||
|
|
||
| while current != nil { | ||
| if let p = p, let q = q { | ||
| if p.val < current!.val && q.val < current!.val { | ||
| current = current?.left | ||
| } else if p.val > current!.val && q.val > current!.val { | ||
| current = current?.right | ||
| } else { | ||
| return current | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return current | ||
| } | ||
| } | ||
|
|
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.
왜 시간복잡도가 O(k)라고 생각하셨는지 궁금합니다..!! k=1이고 왼쪽으로 치우쳐진 트리라면 제일 밑단까지 내려가기 위해 시간이 더 걸리지 않을까 싶어서요!