The k most frequent values
Given a list of integers nums and an integer k, return the k values that appear most often, in any order. k is between 0 and the number of distinct values, and the tests never have a tie at the k-th place, so the answer is unique.
Aim to beat sorting every count when k is small.
Besides the tests, one large input (10,500 numbers, k = 3,500) with a ~300 ms limit (scaled to your device's speed): brute force won't pass.
Examples
top_k_frequent([1,1,1,2,2,3], 2)→[1,2]top_k_frequent([4], 1)→[4]top_k_frequent([4,4,1,1,1,2,2,2,2,3], 3)→[2,1,4]