|
| 1 | +import Foundation |
| 2 | + |
| 3 | +final class MaxHeap<T: Comparable> { |
| 4 | + private var heap: [T] = [] |
| 5 | + |
| 6 | + func insert(_ value: T) { |
| 7 | + heap.append(value) |
| 8 | + heapifyUp(from: heap.count - 1) |
| 9 | + } |
| 10 | + |
| 11 | + @discardableResult |
| 12 | + func pop() -> T? { |
| 13 | + guard !heap.isEmpty else { |
| 14 | + return nil |
| 15 | + } |
| 16 | + |
| 17 | + if heap.count == 1 { |
| 18 | + return heap.removeLast() |
| 19 | + } |
| 20 | + |
| 21 | + let removedValue = heap[0] |
| 22 | + heap[0] = heap.removeLast() |
| 23 | + heapifyDown(from: 0) |
| 24 | + |
| 25 | + return removedValue |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +private extension MaxHeap { |
| 30 | + private func parentIndex(of index: Int) -> Int { |
| 31 | + return (index - 1) / 2 |
| 32 | + } |
| 33 | + |
| 34 | + private func leftChildIndex(of index: Int) -> Int { |
| 35 | + return 2 * index + 1 |
| 36 | + } |
| 37 | + |
| 38 | + private func rightChildIndex(of index: Int) -> Int { |
| 39 | + return 2 * index + 2 |
| 40 | + } |
| 41 | + |
| 42 | + private func heapifyUp(from index: Int) { |
| 43 | + var currentIndex = index |
| 44 | + while currentIndex > 0 { |
| 45 | + let parentIdx = parentIndex(of: currentIndex) |
| 46 | + if heap[currentIndex] > heap[parentIdx] { |
| 47 | + heap.swapAt(currentIndex, parentIdx) |
| 48 | + currentIndex = parentIdx |
| 49 | + } else { |
| 50 | + break |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private func heapifyDown(from index: Int) { |
| 56 | + var oldIndex = index |
| 57 | + let count = heap.count |
| 58 | + |
| 59 | + while true { |
| 60 | + let leftIndex = leftChildIndex(of: oldIndex) |
| 61 | + let rightIndex = rightChildIndex(of: oldIndex) |
| 62 | + var newIndex = oldIndex |
| 63 | + |
| 64 | + if leftIndex < count && heap[leftIndex] > heap[newIndex] { |
| 65 | + newIndex = leftIndex |
| 66 | + } |
| 67 | + |
| 68 | + if rightIndex < count && heap[rightIndex] > heap[newIndex] { |
| 69 | + newIndex = rightIndex |
| 70 | + } |
| 71 | + |
| 72 | + if newIndex == oldIndex { |
| 73 | + break |
| 74 | + } |
| 75 | + |
| 76 | + heap.swapAt(oldIndex, newIndex) |
| 77 | + oldIndex = newIndex |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +struct Item: Comparable { |
| 83 | + let num: Int |
| 84 | + let count: Int |
| 85 | + |
| 86 | + static func < (lhs: Item, rhs: Item) -> Bool { |
| 87 | + return lhs.count <= rhs.count |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +class Solution { |
| 92 | + |
| 93 | + /* |
| 94 | + Runtime: 30 ms (Beats 65.79%) |
| 95 | + Analyze Complexity: O(n log n), n개의 숫자에 대해 heappush, k개의 item에 대해 heappop |
| 96 | + Memory: 17.63 MB (Beats 64.66%) |
| 97 | + */ |
| 98 | + func topKFrequent(_ nums: [Int], _ k: Int) -> [Int] { |
| 99 | + var countDict: [Int: Int] = [:] |
| 100 | + for num in nums { |
| 101 | + countDict[num] = (countDict[num] ?? 0) + 1 |
| 102 | + } |
| 103 | + |
| 104 | + var maxHeap = MaxHeap<Item>() |
| 105 | + for (num, count) in countDict { |
| 106 | + maxHeap.insert(Item(num: num, count: count)) |
| 107 | + } |
| 108 | + |
| 109 | + var result: [Int] = [] |
| 110 | + for _ in 0..<k { |
| 111 | + if let maxItem = maxHeap.pop() { |
| 112 | + result.append(maxItem.num) |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + return result |
| 117 | + } |
| 118 | +} |
0 commit comments