TopKFrequentElements

TopKFrequentElements Difficulty: Medium Topics: Hash Table, Heap, Frequency Counting Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order. Approach This problem asks us to find the k most frequent elements. We first use an unordered_map to count the frequency of each element. Then we use a min-heap of size k to maintain the current top-k elements. When the heap size exceeds k, we remove the smallest element. Finally, we obtain a list of size k that contains the top-k elements. ...

March 2, 2026 · 1 分鐘 · yihan-lee