diff --git a/find-median-from-data-stream/delight010.swift b/find-median-from-data-stream/delight010.swift new file mode 100644 index 000000000..4b6a480fb --- /dev/null +++ b/find-median-from-data-stream/delight010.swift @@ -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()!) + } +} + diff --git a/kth-smallest-element-in-a-bst/delight010.swift b/kth-smallest-element-in-a-bst/delight010.swift new file mode 100644 index 000000000..a67cbf33e --- /dev/null +++ b/kth-smallest-element-in-a-bst/delight010.swift @@ -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) + } +} + diff --git a/lowest-common-ancestor-of-a-binary-search-tree/delight010.swift b/lowest-common-ancestor-of-a-binary-search-tree/delight010.swift new file mode 100644 index 000000000..437932b6b --- /dev/null +++ b/lowest-common-ancestor-of-a-binary-search-tree/delight010.swift @@ -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 + } +} +