From a4521de6b7247203200216caeb90a4812f42b41c Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Tue, 28 May 2024 07:53:13 +0100 Subject: [PATCH 1/2] Swift implementation for LCOF 49 --- .../README.md" | 86 +++++++++++++++++++ .../Solution.swift" | 81 +++++++++++++++++ 2 files changed, 167 insertions(+) create mode 100644 "lcof/\351\235\242\350\257\225\351\242\23049. \344\270\221\346\225\260/Solution.swift" diff --git "a/lcof/\351\235\242\350\257\225\351\242\23049. \344\270\221\346\225\260/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23049. \344\270\221\346\225\260/README.md" index 304d451aaea7f..27673980d97b8 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23049. \344\270\221\346\225\260/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23049. \344\270\221\346\225\260/README.md" @@ -237,6 +237,92 @@ public class Solution { } ``` +#### Swift + +```swift +class Solution { + func nthUglyNumber(_ n: Int) -> Int { + var vis = Set() + var pq = PriorityQueue() + let factors: [Int64] = [2, 3, 5] + + pq.push(1) + vis.insert(1) + var ans: Int64 = 0 + + for _ in 0.. { + private var heap: [T] = [] + + var isEmpty: Bool { + return heap.isEmpty + } + + mutating func push(_ element: T) { + heap.append(element) + heapifyUp(from: heap.count - 1) + } + + mutating func pop() -> T? { + guard !heap.isEmpty else { + return nil + } + if heap.count == 1 { + return heap.removeLast() + } + let value = heap[0] + heap[0] = heap.removeLast() + heapifyDown(from: 0) + return value + } + + private mutating func heapifyUp(from index: Int) { + var index = index + let element = heap[index] + while index > 0 { + let parentIndex = (index - 1) / 2 + if element >= heap[parentIndex] { + break + } + heap[index] = heap[parentIndex] + index = parentIndex + } + heap[index] = element + } + + private mutating func heapifyDown(from index: Int) { + var index = index + let element = heap[index] + let count = heap.count + while index < count / 2 { + var childIndex = index * 2 + 1 + if childIndex + 1 < count && heap[childIndex + 1] < heap[childIndex] { + childIndex += 1 + } + if element <= heap[childIndex] { + break + } + heap[index] = heap[childIndex] + index = childIndex + } + heap[index] = element + } +} +``` + diff --git "a/lcof/\351\235\242\350\257\225\351\242\23049. \344\270\221\346\225\260/Solution.swift" "b/lcof/\351\235\242\350\257\225\351\242\23049. \344\270\221\346\225\260/Solution.swift" new file mode 100644 index 0000000000000..04dfddcd4b3da --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23049. \344\270\221\346\225\260/Solution.swift" @@ -0,0 +1,81 @@ +class Solution { + func nthUglyNumber(_ n: Int) -> Int { + var vis = Set() + var pq = PriorityQueue() + let factors: [Int64] = [2, 3, 5] + + pq.push(1) + vis.insert(1) + var ans: Int64 = 0 + + for _ in 0.. { + private var heap: [T] = [] + + var isEmpty: Bool { + return heap.isEmpty + } + + mutating func push(_ element: T) { + heap.append(element) + heapifyUp(from: heap.count - 1) + } + + mutating func pop() -> T? { + guard !heap.isEmpty else { + return nil + } + if heap.count == 1 { + return heap.removeLast() + } + let value = heap[0] + heap[0] = heap.removeLast() + heapifyDown(from: 0) + return value + } + + private mutating func heapifyUp(from index: Int) { + var index = index + let element = heap[index] + while index > 0 { + let parentIndex = (index - 1) / 2 + if element >= heap[parentIndex] { + break + } + heap[index] = heap[parentIndex] + index = parentIndex + } + heap[index] = element + } + + private mutating func heapifyDown(from index: Int) { + var index = index + let element = heap[index] + let count = heap.count + while index < count / 2 { + var childIndex = index * 2 + 1 + if childIndex + 1 < count && heap[childIndex + 1] < heap[childIndex] { + childIndex += 1 + } + if element <= heap[childIndex] { + break + } + heap[index] = heap[childIndex] + index = childIndex + } + heap[index] = element + } +} \ No newline at end of file From 4a05bd52be9e46d3bf749b32e34aa84325e099aa Mon Sep 17 00:00:00 2001 From: yanglbme Date: Tue, 28 May 2024 09:34:32 +0000 Subject: [PATCH 2/2] style: format code and docs with prettier --- .../README.md" | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git "a/lcof/\351\235\242\350\257\225\351\242\23049. \344\270\221\346\225\260/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23049. \344\270\221\346\225\260/README.md" index 27673980d97b8..89aef3320acbd 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23049. \344\270\221\346\225\260/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23049. \344\270\221\346\225\260/README.md" @@ -245,11 +245,11 @@ class Solution { var vis = Set() var pq = PriorityQueue() let factors: [Int64] = [2, 3, 5] - + pq.push(1) vis.insert(1) var ans: Int64 = 0 - + for _ in 0.. { private var heap: [T] = [] - + var isEmpty: Bool { return heap.isEmpty } - + mutating func push(_ element: T) { heap.append(element) heapifyUp(from: heap.count - 1) } - + mutating func pop() -> T? { guard !heap.isEmpty else { return nil @@ -288,7 +288,7 @@ struct PriorityQueue { heapifyDown(from: 0) return value } - + private mutating func heapifyUp(from index: Int) { var index = index let element = heap[index] @@ -302,7 +302,7 @@ struct PriorityQueue { } heap[index] = element } - + private mutating func heapifyDown(from index: Int) { var index = index let element = heap[index]