From 8da67f037e673d06c5674428330661dd9ff12216 Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Thu, 13 Jun 2024 16:30:36 +0100 Subject: [PATCH 1/4] feat: add swift implementation to lcof2 problem: No.059 --- .../README.md" | 34 +++++++++++++++++++ .../Solution.swift" | 29 ++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 "lcof2/\345\211\221\346\214\207 Offer II 059. \346\225\260\346\215\256\346\265\201\347\232\204\347\254\254 K \345\244\247\346\225\260\345\200\274/Solution.swift" diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 059. \346\225\260\346\215\256\346\265\201\347\232\204\347\254\254 K \345\244\247\346\225\260\345\200\274/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 059. \346\225\260\346\215\256\346\265\201\347\232\204\347\254\254 K \345\244\247\346\225\260\345\200\274/README.md" index 493a1956e73a9..aada439879f1d 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 059. \346\225\260\346\215\256\346\265\201\347\232\204\347\254\254 K \345\244\247\346\225\260\345\200\274/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 059. \346\225\260\346\215\256\346\265\201\347\232\204\347\254\254 K \345\244\247\346\225\260\345\200\274/README.md" @@ -225,6 +225,40 @@ func (h *IntHeap) Top() int { */ ``` +#### Swift + +```swift +class KthLargest { + private var minHeap: [Int] + private var size: Int + + init(_ k: Int, _ nums: [Int]) { + size = k + minHeap = [] + for num in nums { + add(num) + } + } + + func add(_ val: Int) -> Int { + if minHeap.count < size { + minHeap.append(val) + minHeap.sort() + } else if val > minHeap[0] { + minHeap[0] = val + minHeap.sort() + } + return minHeap[0] + } +} + +/** + * Your KthLargest object will be instantiated and called as such: + * let obj = KthLargest(k, nums) + * let ret = obj.add(val) + */ +``` + diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 059. \346\225\260\346\215\256\346\265\201\347\232\204\347\254\254 K \345\244\247\346\225\260\345\200\274/Solution.swift" "b/lcof2/\345\211\221\346\214\207 Offer II 059. \346\225\260\346\215\256\346\265\201\347\232\204\347\254\254 K \345\244\247\346\225\260\345\200\274/Solution.swift" new file mode 100644 index 0000000000000..573029fca9105 --- /dev/null +++ "b/lcof2/\345\211\221\346\214\207 Offer II 059. \346\225\260\346\215\256\346\265\201\347\232\204\347\254\254 K \345\244\247\346\225\260\345\200\274/Solution.swift" @@ -0,0 +1,29 @@ +class KthLargest { + private var minHeap: [Int] + private var size: Int + + init(_ k: Int, _ nums: [Int]) { + size = k + minHeap = [] + for num in nums { + add(num) + } + } + + func add(_ val: Int) -> Int { + if minHeap.count < size { + minHeap.append(val) + minHeap.sort() + } else if val > minHeap[0] { + minHeap[0] = val + minHeap.sort() + } + return minHeap[0] + } +} + +/** + * Your KthLargest object will be instantiated and called as such: + * let obj = KthLargest(k, nums) + * let ret = obj.add(val) + */ \ No newline at end of file From 266c2f653740abc113b0a3921cc864230aa7c584 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 14 Jun 2024 08:29:49 +0800 Subject: [PATCH 2/4] Update README.md --- .../README.md" | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 059. \346\225\260\346\215\256\346\265\201\347\232\204\347\254\254 K \345\244\247\346\225\260\345\200\274/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 059. \346\225\260\346\215\256\346\265\201\347\232\204\347\254\254 K \345\244\247\346\225\260\345\200\274/README.md" index aada439879f1d..fd76efb11de11 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 059. \346\225\260\346\215\256\346\265\201\347\232\204\347\254\254 K \345\244\247\346\225\260\345\200\274/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 059. \346\225\260\346\215\256\346\265\201\347\232\204\347\254\254 K \345\244\247\346\225\260\345\200\274/README.md" @@ -228,34 +228,33 @@ func (h *IntHeap) Top() int { #### Swift ```swift +import Collections + class KthLargest { - private var minHeap: [Int] + private var h: Heap private var size: Int init(_ k: Int, _ nums: [Int]) { + h = Heap() size = k - minHeap = [] - for num in nums { - add(num) + for x in nums { + add(x) } } - + func add(_ val: Int) -> Int { - if minHeap.count < size { - minHeap.append(val) - minHeap.sort() - } else if val > minHeap[0] { - minHeap[0] = val - minHeap.sort() + h.insert(val) + if h.count > size { + h.removeMin() } - return minHeap[0] + return h.min! } } /** * Your KthLargest object will be instantiated and called as such: * let obj = KthLargest(k, nums) - * let ret = obj.add(val) + * let ret_1: Int = obj.add(val) */ ``` From 703f81fca73a4178f0a28f8a48714fc1bb85a748 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 14 Jun 2024 08:30:10 +0800 Subject: [PATCH 3/4] Update Solution.swift --- .../Solution.swift" | 27 +++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 059. \346\225\260\346\215\256\346\265\201\347\232\204\347\254\254 K \345\244\247\346\225\260\345\200\274/Solution.swift" "b/lcof2/\345\211\221\346\214\207 Offer II 059. \346\225\260\346\215\256\346\265\201\347\232\204\347\254\254 K \345\244\247\346\225\260\345\200\274/Solution.swift" index 573029fca9105..0c81b4625ede2 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 059. \346\225\260\346\215\256\346\265\201\347\232\204\347\254\254 K \345\244\247\346\225\260\345\200\274/Solution.swift" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 059. \346\225\260\346\215\256\346\265\201\347\232\204\347\254\254 K \345\244\247\346\225\260\345\200\274/Solution.swift" @@ -1,29 +1,28 @@ +import Collections + class KthLargest { - private var minHeap: [Int] + private var h: Heap private var size: Int init(_ k: Int, _ nums: [Int]) { + h = Heap() size = k - minHeap = [] - for num in nums { - add(num) + for x in nums { + add(x) } } - + func add(_ val: Int) -> Int { - if minHeap.count < size { - minHeap.append(val) - minHeap.sort() - } else if val > minHeap[0] { - minHeap[0] = val - minHeap.sort() + h.insert(val) + if h.count > size { + h.removeMin() } - return minHeap[0] + return h.min! } } /** * Your KthLargest object will be instantiated and called as such: * let obj = KthLargest(k, nums) - * let ret = obj.add(val) - */ \ No newline at end of file + * let ret_1: Int = obj.add(val) + */ From f79b2e2dec50077f970578afa42ff6d46fb746ce Mon Sep 17 00:00:00 2001 From: yanglbme Date: Fri, 14 Jun 2024 00:32:36 +0000 Subject: [PATCH 4/4] 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 059. \346\225\260\346\215\256\346\265\201\347\232\204\347\254\254 K \345\244\247\346\225\260\345\200\274/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 059. \346\225\260\346\215\256\346\265\201\347\232\204\347\254\254 K \345\244\247\346\225\260\345\200\274/README.md" index fd76efb11de11..2edc01d347d04 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 059. \346\225\260\346\215\256\346\265\201\347\232\204\347\254\254 K \345\244\247\346\225\260\345\200\274/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 059. \346\225\260\346\215\256\346\265\201\347\232\204\347\254\254 K \345\244\247\346\225\260\345\200\274/README.md" @@ -241,7 +241,7 @@ class KthLargest { add(x) } } - + func add(_ val: Int) -> Int { h.insert(val) if h.count > size {