From 672bb8e489c4c1b4bee9564a360ecb68e39257e7 Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Thu, 13 Jun 2024 16:37:53 +0100 Subject: [PATCH 1/5] feat: add swift implementation to lcof2 problem: No.060 --- .../README.md" | 101 ++++++++++++++++++ .../Solution.swift" | 97 +++++++++++++++++ 2 files changed, 198 insertions(+) create mode 100644 "lcof2/\345\211\221\346\214\207 Offer II 060. \345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204 k \344\270\252\346\225\260\345\255\227/Solution.swift" diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 060. \345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204 k \344\270\252\346\225\260\345\255\227/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 060. \345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204 k \344\270\252\346\225\260\345\255\227/README.md" index bb48542187c42..daae1bf72b460 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 060. \345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204 k \344\270\252\346\225\260\345\255\227/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 060. \345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204 k \344\270\252\346\225\260\345\255\227/README.md" @@ -198,6 +198,107 @@ impl Solution { } ``` +#### Swift + +```swift +class Solution { + func topKFrequent(_ nums: [Int], _ k: Int) -> [Int] { + var frequency: [Int: Int] = [:] + + for num in nums { + frequency[num, default: 0] += 1 + } + + var queue = PriorityQueue<(Int, Int)>(sort: { $0.1 < $1.1 }) + + for entry in frequency { + queue.enqueue((entry.key, entry.value)) + if queue.count > k { + _ = queue.dequeue() + } + } + + return queue.toArray().map { $0.0 } + } +} + +public struct PriorityQueue { + private var heap: [Element] + private let sort: (Element, Element) -> Bool + + public init(sort: @escaping (Element, Element) -> Bool) { + self.heap = [] + self.sort = sort + } + + public var isEmpty: Bool { + return heap.isEmpty + } + + public var count: Int { + return heap.count + } + + public func peek() -> Element? { + return heap.first + } + + public mutating func enqueue(_ element: Element) { + heap.append(element) + siftUp(heap.count - 1) + } + + public mutating func dequeue() -> Element? { + guard !heap.isEmpty else { return nil } + heap.swapAt(0, heap.count - 1) + let element = heap.removeLast() + siftDown(0) + return element + } + + public func toArray() -> [Element] { + return heap + } + + private mutating func siftUp(_ index: Int) { + var index = index + while index > 0 { + let parentIndex = (index - 1) / 2 + if sort(heap[index], heap[parentIndex]) { + heap.swapAt(index, parentIndex) + index = parentIndex + } else { + return + } + } + } + + private mutating func siftDown(_ index: Int) { + var index = index + let count = heap.count + let element = heap[index] + + while true { + let leftChildIndex = 2 * index + 1 + let rightChildIndex = 2 * index + 2 + var first = index + + if leftChildIndex < count && sort(heap[leftChildIndex], heap[first]) { + first = leftChildIndex + } + if rightChildIndex < count && sort(heap[rightChildIndex], heap[first]) { + first = rightChildIndex + } + if first == index { return } + + heap[index] = heap[first] + heap[first] = element + index = first + } + } +} +``` + diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 060. \345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204 k \344\270\252\346\225\260\345\255\227/Solution.swift" "b/lcof2/\345\211\221\346\214\207 Offer II 060. \345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204 k \344\270\252\346\225\260\345\255\227/Solution.swift" new file mode 100644 index 0000000000000..768d3152e29bf --- /dev/null +++ "b/lcof2/\345\211\221\346\214\207 Offer II 060. \345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204 k \344\270\252\346\225\260\345\255\227/Solution.swift" @@ -0,0 +1,97 @@ +class Solution { + func topKFrequent(_ nums: [Int], _ k: Int) -> [Int] { + var frequency: [Int: Int] = [:] + + for num in nums { + frequency[num, default: 0] += 1 + } + + var queue = PriorityQueue<(Int, Int)>(sort: { $0.1 < $1.1 }) + + for entry in frequency { + queue.enqueue((entry.key, entry.value)) + if queue.count > k { + _ = queue.dequeue() + } + } + + return queue.toArray().map { $0.0 } + } +} + +public struct PriorityQueue { + private var heap: [Element] + private let sort: (Element, Element) -> Bool + + public init(sort: @escaping (Element, Element) -> Bool) { + self.heap = [] + self.sort = sort + } + + public var isEmpty: Bool { + return heap.isEmpty + } + + public var count: Int { + return heap.count + } + + public func peek() -> Element? { + return heap.first + } + + public mutating func enqueue(_ element: Element) { + heap.append(element) + siftUp(heap.count - 1) + } + + public mutating func dequeue() -> Element? { + guard !heap.isEmpty else { return nil } + heap.swapAt(0, heap.count - 1) + let element = heap.removeLast() + siftDown(0) + return element + } + + public func toArray() -> [Element] { + return heap + } + + private mutating func siftUp(_ index: Int) { + var index = index + while index > 0 { + let parentIndex = (index - 1) / 2 + if sort(heap[index], heap[parentIndex]) { + heap.swapAt(index, parentIndex) + index = parentIndex + } else { + return + } + } + } + + private mutating func siftDown(_ index: Int) { + var index = index + let count = heap.count + let element = heap[index] + + while true { + let leftChildIndex = 2 * index + 1 + let rightChildIndex = 2 * index + 2 + var first = index + + if leftChildIndex < count && sort(heap[leftChildIndex], heap[first]) { + first = leftChildIndex + } + if rightChildIndex < count && sort(heap[rightChildIndex], heap[first]) { + first = rightChildIndex + } + if first == index { return } + + heap[index] = heap[first] + heap[first] = element + index = first + } + } +} + From e34fd1169e0f8a0d6cb02e0fa25fec2a1addd10d Mon Sep 17 00:00:00 2001 From: klever34 Date: Thu, 13 Jun 2024 15:46:27 +0000 Subject: [PATCH 2/5] style: format code and docs with prettier --- .../README.md" | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 060. \345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204 k \344\270\252\346\225\260\345\255\227/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 060. \345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204 k \344\270\252\346\225\260\345\255\227/README.md" index daae1bf72b460..7983dc63e7929 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 060. \345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204 k \344\270\252\346\225\260\345\255\227/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 060. \345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204 k \344\270\252\346\225\260\345\255\227/README.md" @@ -204,20 +204,20 @@ impl Solution { class Solution { func topKFrequent(_ nums: [Int], _ k: Int) -> [Int] { var frequency: [Int: Int] = [:] - + for num in nums { frequency[num, default: 0] += 1 } - + var queue = PriorityQueue<(Int, Int)>(sort: { $0.1 < $1.1 }) - + for entry in frequency { queue.enqueue((entry.key, entry.value)) if queue.count > k { _ = queue.dequeue() } } - + return queue.toArray().map { $0.0 } } } @@ -225,29 +225,29 @@ class Solution { public struct PriorityQueue { private var heap: [Element] private let sort: (Element, Element) -> Bool - + public init(sort: @escaping (Element, Element) -> Bool) { self.heap = [] self.sort = sort } - + public var isEmpty: Bool { return heap.isEmpty } - + public var count: Int { return heap.count } - + public func peek() -> Element? { return heap.first } - + public mutating func enqueue(_ element: Element) { heap.append(element) siftUp(heap.count - 1) } - + public mutating func dequeue() -> Element? { guard !heap.isEmpty else { return nil } heap.swapAt(0, heap.count - 1) @@ -255,11 +255,11 @@ public struct PriorityQueue { siftDown(0) return element } - + public func toArray() -> [Element] { return heap } - + private mutating func siftUp(_ index: Int) { var index = index while index > 0 { @@ -272,17 +272,17 @@ public struct PriorityQueue { } } } - + private mutating func siftDown(_ index: Int) { var index = index let count = heap.count let element = heap[index] - + while true { let leftChildIndex = 2 * index + 1 let rightChildIndex = 2 * index + 2 var first = index - + if leftChildIndex < count && sort(heap[leftChildIndex], heap[first]) { first = leftChildIndex } @@ -290,7 +290,7 @@ public struct PriorityQueue { first = rightChildIndex } if first == index { return } - + heap[index] = heap[first] heap[first] = element index = first From 7d025a04b7d270ed0f83eb4d23def8da4a1e3684 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 14 Jun 2024 08:40:29 +0800 Subject: [PATCH 3/5] Update README.md --- .../README.md" | 100 ++++-------------- 1 file changed, 20 insertions(+), 80 deletions(-) diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 060. \345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204 k \344\270\252\346\225\260\345\255\227/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 060. \345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204 k \344\270\252\346\225\260\345\255\227/README.md" index 7983dc63e7929..499a9f2f07411 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 060. \345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204 k \344\270\252\346\225\260\345\255\227/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 060. \345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204 k \344\270\252\346\225\260\345\255\227/README.md" @@ -201,100 +201,40 @@ impl Solution { #### Swift ```swift +import HeapModule + class Solution { func topKFrequent(_ nums: [Int], _ k: Int) -> [Int] { var frequency: [Int: Int] = [:] - for num in nums { frequency[num, default: 0] += 1 } - - var queue = PriorityQueue<(Int, Int)>(sort: { $0.1 < $1.1 }) - - for entry in frequency { - queue.enqueue((entry.key, entry.value)) - if queue.count > k { - _ = queue.dequeue() + + var freqHeap = Heap() + for (key, value) in frequency { + freqHeap.insert(.init(val: key, freq: value)) + if freqHeap.count > k { + freqHeap.removeMin() } } - - return queue.toArray().map { $0.0 } + var ans = [Int]() + while let element = freqHeap.popMax() { + ans.append(element.val) + } + return ans } } -public struct PriorityQueue { - private var heap: [Element] - private let sort: (Element, Element) -> Bool - - public init(sort: @escaping (Element, Element) -> Bool) { - self.heap = [] - self.sort = sort - } - - public var isEmpty: Bool { - return heap.isEmpty - } - - public var count: Int { - return heap.count - } - - public func peek() -> Element? { - return heap.first - } - - public mutating func enqueue(_ element: Element) { - heap.append(element) - siftUp(heap.count - 1) - } - - public mutating func dequeue() -> Element? { - guard !heap.isEmpty else { return nil } - heap.swapAt(0, heap.count - 1) - let element = heap.removeLast() - siftDown(0) - return element - } +struct FreqElement: Comparable { + let val: Int + let freq: Int - public func toArray() -> [Element] { - return heap - } - - private mutating func siftUp(_ index: Int) { - var index = index - while index > 0 { - let parentIndex = (index - 1) / 2 - if sort(heap[index], heap[parentIndex]) { - heap.swapAt(index, parentIndex) - index = parentIndex - } else { - return - } - } + static func < (lhs: FreqElement, rhs: FreqElement) -> Bool { + lhs.freq < rhs.freq } - private mutating func siftDown(_ index: Int) { - var index = index - let count = heap.count - let element = heap[index] - - while true { - let leftChildIndex = 2 * index + 1 - let rightChildIndex = 2 * index + 2 - var first = index - - if leftChildIndex < count && sort(heap[leftChildIndex], heap[first]) { - first = leftChildIndex - } - if rightChildIndex < count && sort(heap[rightChildIndex], heap[first]) { - first = rightChildIndex - } - if first == index { return } - - heap[index] = heap[first] - heap[first] = element - index = first - } + static func == (lhs: FreqElement, rhs: FreqElement) -> Bool { + lhs.freq == rhs.freq } } ``` From 953baefe94a992b2ce27b17ea0314821d97b232b Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 14 Jun 2024 08:40:51 +0800 Subject: [PATCH 4/5] Update Solution.swift --- .../Solution.swift" | 103 ++++-------------- 1 file changed, 21 insertions(+), 82 deletions(-) diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 060. \345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204 k \344\270\252\346\225\260\345\255\227/Solution.swift" "b/lcof2/\345\211\221\346\214\207 Offer II 060. \345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204 k \344\270\252\346\225\260\345\255\227/Solution.swift" index 768d3152e29bf..1a67b4e79fe9b 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 060. \345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204 k \344\270\252\346\225\260\345\255\227/Solution.swift" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 060. \345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204 k \344\270\252\346\225\260\345\255\227/Solution.swift" @@ -1,97 +1,36 @@ +import HeapModule + class Solution { func topKFrequent(_ nums: [Int], _ k: Int) -> [Int] { var frequency: [Int: Int] = [:] - for num in nums { frequency[num, default: 0] += 1 } - var queue = PriorityQueue<(Int, Int)>(sort: { $0.1 < $1.1 }) - - for entry in frequency { - queue.enqueue((entry.key, entry.value)) - if queue.count > k { - _ = queue.dequeue() + var freqHeap = Heap() + for (key, value) in frequency { + freqHeap.insert(.init(val: key, freq: value)) + if freqHeap.count > k { + freqHeap.removeMin() } } - - return queue.toArray().map { $0.0 } + var ans = [Int]() + while let element = freqHeap.popMax() { + ans.append(element.val) + } + return ans } } -public struct PriorityQueue { - private var heap: [Element] - private let sort: (Element, Element) -> Bool - - public init(sort: @escaping (Element, Element) -> Bool) { - self.heap = [] - self.sort = sort - } - - public var isEmpty: Bool { - return heap.isEmpty - } - - public var count: Int { - return heap.count - } - - public func peek() -> Element? { - return heap.first - } - - public mutating func enqueue(_ element: Element) { - heap.append(element) - siftUp(heap.count - 1) - } - - public mutating func dequeue() -> Element? { - guard !heap.isEmpty else { return nil } - heap.swapAt(0, heap.count - 1) - let element = heap.removeLast() - siftDown(0) - return element - } - - public func toArray() -> [Element] { - return heap - } - - private mutating func siftUp(_ index: Int) { - var index = index - while index > 0 { - let parentIndex = (index - 1) / 2 - if sort(heap[index], heap[parentIndex]) { - heap.swapAt(index, parentIndex) - index = parentIndex - } else { - return - } - } +struct FreqElement: Comparable { + let val: Int + let freq: Int + + static func < (lhs: FreqElement, rhs: FreqElement) -> Bool { + lhs.freq < rhs.freq } - - private mutating func siftDown(_ index: Int) { - var index = index - let count = heap.count - let element = heap[index] - - while true { - let leftChildIndex = 2 * index + 1 - let rightChildIndex = 2 * index + 2 - var first = index - - if leftChildIndex < count && sort(heap[leftChildIndex], heap[first]) { - first = leftChildIndex - } - if rightChildIndex < count && sort(heap[rightChildIndex], heap[first]) { - first = rightChildIndex - } - if first == index { return } - - heap[index] = heap[first] - heap[first] = element - index = first - } + + static func == (lhs: FreqElement, rhs: FreqElement) -> Bool { + lhs.freq == rhs.freq } } - From ac211850e12d1bb11b5571fd7c5417a7ddcef67f Mon Sep 17 00:00:00 2001 From: yanglbme Date: Fri, 14 Jun 2024 00:41:46 +0000 Subject: [PATCH 5/5] style: format code and docs with prettier --- .../README.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 060. \345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204 k \344\270\252\346\225\260\345\255\227/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 060. \345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204 k \344\270\252\346\225\260\345\255\227/README.md" index 499a9f2f07411..73dc2ba798415 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 060. \345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204 k \344\270\252\346\225\260\345\255\227/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 060. \345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204 k \344\270\252\346\225\260\345\255\227/README.md" @@ -209,7 +209,7 @@ class Solution { for num in nums { frequency[num, default: 0] += 1 } - + var freqHeap = Heap() for (key, value) in frequency { freqHeap.insert(.init(val: key, freq: value))