Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 139 additions & 0 deletions find-median-from-data-stream/delight010.swift
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()!)
}
}

31 changes: 31 additions & 0 deletions kth-smallest-element-in-a-bst/delight010.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Solution {
// Time O(k)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

왜 시간복잡도가 O(k)라고 생각하셨는지 궁금합니다..!! k=1이고 왼쪽으로 치우쳐진 트리라면 제일 밑단까지 내려가기 위해 시간이 더 걸리지 않을까 싶어서요!

// 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 lowest-common-ancestor-of-a-binary-search-tree/delight010.swift
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
}
}