[swift] || Using Heap Sort  || Easy explanation
The solution follows the idea of using a maxHeap, wherein the root object is the object with the highest value. \n\nA maxHeap also has a property that every chi
The solution follows the idea of using a maxHeap, wherein the root object is the object with the highest value. \n\nA maxHeap also has a property that every child of a node will have a lower weight than its parent, so while it does not deliver ordered sorting, the elements are in a semi sorted order. \n\nThe simplest fix here is to pass the incoming array thru a heapify construct, which transforms the input array into a maxHeap, and then extract the top 2 heaviest elements from the heap. \n\n`Time Complexity :: worst case :: O(n lg(n))`\n\n```\nclass Solution {    \n    func maxProduct(_ nums: [Int]) -> Int {\n        let heap = heapify(nums)               \n        if heap.count > 2 {\n            let first = heap[0]\n            let second = heap[1] > heap[2] ? heap[1] : heap[2]\n            return (first - 1) * (second - 1)\n        } else {\n            return (heap[0] - 1) * (heap[1] - 1)\n        }\n        \n    }\n    \n    func heapify(_ nums: [Int]) -> [Int] {\n        var nums = nums\n        var newNums = [Int]()\n        let maxCount = nums.count\n        for i in 0..<maxCount {            \n            newNums.append(nums[i])\n            var j = i\n            var parentIdx = (j-1)/2\n            while parentIdx >= 0, newNums[parentIdx] < newNums[j] {                \n                let temp = newNums[j]\n                newNums[j] = newNums[parentIdx]\n                newNums[parentIdx] = temp\n                j = parentIdx\n                parentIdx = (j-1)/2                \n            }\n        }\n        return newNums\n    }\n}\n```
['Swift', 'Heap (Priority Queue)']