Chapter 25: Priority Queue Challenges

Challenge 1:

You have learned to use a heap to construct a priority queue by conforming to the Queue protocol. Now, construct a priority queue using an Array.

  • 思路: 用array來做Priority Queue, 就是把array的內容先排序一次

Within the init method, you leverage the array's sort function. Accord to Apple the sort function takes O(n log n) time.

public struct PriorityQueueArray<T: Equatable>: Queue {

    // 利用array來存elements
    private var elements: [T] = []
    let sort: (Element, Element) -> Bool

    public var isEmpty: Bool {
        return elements.isEmpty
    }

    public var peek: T? {
        return elements.first
    }

    // 建立init
    public init(sort: @escaping(Element, Element) -> Bool, elements: [Element] = []) {
        self.sort = sort
        self.elements = elements
        // 將element進行排序
        // Accord to Apple the sort function takes O(n log n) time.
        // Swift's sort function uses introsort which is a combination of insertion sort and heap sort.
        self.elements.sort(by: sort)
    }

    public mutating func enqueue(_ element: T) -> Bool {
        //  O(n)
        for (index, otherElement) in elements.enumerated() {
            // 檢查element是否有更高priority
            if sort(element, otherElement) {
                elements.insert(element, at: index)
                return true
            }
        }
        // If the element does not have a higher priority than any element in the queue, simply append the element to the end
        elements.append(element)
        return true
    }

    public mutating func dequeue() -> T? {
        // O(n) operation, since you have to shift the existing elements to the left by one.
        return isEmpty ? nil : elements.removeFirst()
    }

}

Challenge 2:

Your favorite T-Swift concert was sold out. Fortunately there is a waitlist for people who still want to go! However the ticket sales will first prioritize someone with a military background, followed by seniority. Write a sort function that will return the list of people on the waitlist by the priority mentioned. The Person struct is provided below:

  • 思路: 如何寫一個自己客製化的sort

Last updated