Skip to content

Commit 953baef

Browse files
authored
Update Solution.swift
1 parent 7d025a0 commit 953baef

File tree

1 file changed

+21
-82
lines changed
  • lcof2/剑指 Offer II 060. 出现频率最高的 k 个数字

1 file changed

+21
-82
lines changed
Lines changed: 21 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,36 @@
1+
import HeapModule
2+
13
class Solution {
24
func topKFrequent(_ nums: [Int], _ k: Int) -> [Int] {
35
var frequency: [Int: Int] = [:]
4-
56
for num in nums {
67
frequency[num, default: 0] += 1
78
}
89

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()
1515
}
1616
}
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
1922
}
2023
}
2124

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
7131
}
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
9535
}
9636
}
97-

0 commit comments

Comments
 (0)