|
| 1 | +import HeapModule |
| 2 | + |
1 | 3 | class Solution {
|
2 | 4 | func topKFrequent(_ nums: [Int], _ k: Int) -> [Int] {
|
3 | 5 | var frequency: [Int: Int] = [:]
|
4 |
| - |
5 | 6 | for num in nums {
|
6 | 7 | frequency[num, default: 0] += 1
|
7 | 8 | }
|
8 | 9 |
|
9 |
| - var queue = PriorityQueue<(Int, Int)>(sort: { $0.1 < $1.1 }) |
10 |
| - |
11 |
| - for entry in frequency { |
12 |
| - queue.enqueue((entry.key, entry.value)) |
13 |
| - if queue.count > k { |
14 |
| - _ = queue.dequeue() |
| 10 | + var freqHeap = Heap<FreqElement>() |
| 11 | + for (key, value) in frequency { |
| 12 | + freqHeap.insert(.init(val: key, freq: value)) |
| 13 | + if freqHeap.count > k { |
| 14 | + freqHeap.removeMin() |
15 | 15 | }
|
16 | 16 | }
|
17 |
| - |
18 |
| - return queue.toArray().map { $0.0 } |
| 17 | + var ans = [Int]() |
| 18 | + while let element = freqHeap.popMax() { |
| 19 | + ans.append(element.val) |
| 20 | + } |
| 21 | + return ans |
19 | 22 | }
|
20 | 23 | }
|
21 | 24 |
|
22 |
| -public struct PriorityQueue<Element> { |
23 |
| - private var heap: [Element] |
24 |
| - private let sort: (Element, Element) -> Bool |
25 |
| - |
26 |
| - public init(sort: @escaping (Element, Element) -> Bool) { |
27 |
| - self.heap = [] |
28 |
| - self.sort = sort |
29 |
| - } |
30 |
| - |
31 |
| - public var isEmpty: Bool { |
32 |
| - return heap.isEmpty |
33 |
| - } |
34 |
| - |
35 |
| - public var count: Int { |
36 |
| - return heap.count |
37 |
| - } |
38 |
| - |
39 |
| - public func peek() -> Element? { |
40 |
| - return heap.first |
41 |
| - } |
42 |
| - |
43 |
| - public mutating func enqueue(_ element: Element) { |
44 |
| - heap.append(element) |
45 |
| - siftUp(heap.count - 1) |
46 |
| - } |
47 |
| - |
48 |
| - public mutating func dequeue() -> Element? { |
49 |
| - guard !heap.isEmpty else { return nil } |
50 |
| - heap.swapAt(0, heap.count - 1) |
51 |
| - let element = heap.removeLast() |
52 |
| - siftDown(0) |
53 |
| - return element |
54 |
| - } |
55 |
| - |
56 |
| - public func toArray() -> [Element] { |
57 |
| - return heap |
58 |
| - } |
59 |
| - |
60 |
| - private mutating func siftUp(_ index: Int) { |
61 |
| - var index = index |
62 |
| - while index > 0 { |
63 |
| - let parentIndex = (index - 1) / 2 |
64 |
| - if sort(heap[index], heap[parentIndex]) { |
65 |
| - heap.swapAt(index, parentIndex) |
66 |
| - index = parentIndex |
67 |
| - } else { |
68 |
| - return |
69 |
| - } |
70 |
| - } |
| 25 | +struct FreqElement: Comparable { |
| 26 | + let val: Int |
| 27 | + let freq: Int |
| 28 | + |
| 29 | + static func < (lhs: FreqElement, rhs: FreqElement) -> Bool { |
| 30 | + lhs.freq < rhs.freq |
71 | 31 | }
|
72 |
| - |
73 |
| - private mutating func siftDown(_ index: Int) { |
74 |
| - var index = index |
75 |
| - let count = heap.count |
76 |
| - let element = heap[index] |
77 |
| - |
78 |
| - while true { |
79 |
| - let leftChildIndex = 2 * index + 1 |
80 |
| - let rightChildIndex = 2 * index + 2 |
81 |
| - var first = index |
82 |
| - |
83 |
| - if leftChildIndex < count && sort(heap[leftChildIndex], heap[first]) { |
84 |
| - first = leftChildIndex |
85 |
| - } |
86 |
| - if rightChildIndex < count && sort(heap[rightChildIndex], heap[first]) { |
87 |
| - first = rightChildIndex |
88 |
| - } |
89 |
| - if first == index { return } |
90 |
| - |
91 |
| - heap[index] = heap[first] |
92 |
| - heap[first] = element |
93 |
| - index = first |
94 |
| - } |
| 32 | + |
| 33 | + static func == (lhs: FreqElement, rhs: FreqElement) -> Bool { |
| 34 | + lhs.freq == rhs.freq |
95 | 35 | }
|
96 | 36 | }
|
97 |
| - |
0 commit comments