Link: https://leetcode.com/problems/top-k-frequent-elements/
Solution:
Topics: heap
Intuition
Most top k problems will be related to heap. This one is very simple. The idea is to just push a tuple (count, num)
onto a heap a pop off the heap k times and store the result. Using heap, we bring down the complexity to o(klogn)
compared to o(nlogn)
using sort.
Implementation
Review 1
Cute little problem. Solved in 10 seconds.