Return the k most frequent values in nums.
Output order (deterministic)
Sorting every distinct value is O(d log d). The point of this exercise is to do
better when k is small: count with a hash table, then keep only the bestk with a size-k min-heap (O(d log k)), or with a bucket sort (O(n)).
Examples
solve([1, 1, 1, 2, 2, 3], 2) → [1, 2]
solve([1], 1) → [1]
solve([4, 4, 5, 5, 6], 2) → [4, 5] // 4 and 5 tie, smaller first
solve([-1, -1, 2, 2, 3], 3) → [-1, 2, 3]Constraints
1 <= k <= the number of distinct values.Sample tests