@@ -0,0 +1,1557 @@
+%22%22%22%0AGiven a non-empty list of words, return the k most frequent elements.%0A%0AYour answer should be sorted by frequency from highest to lowest. If two words%0Ahave the same frequency, then the word with the lower alphabetical order comes%0Afirst.%0A%0AExample 1:%0A    Input: %5B%22i%22, %22love%22, %22leetcode%22, %22i%22, %22love%22, %22coding%22%5D, k = 2%0A    Output: %5B%22i%22, %22love%22%5D%0A%0AExplanation: %22i%22 and %22love%22 are the two most frequent words.%0A    Note that %22i%22 comes before %22love%22 due to a lower alphabetical order.%0A%0AExample 2:%0A    Input: %5B%22the%22, %22day%22, %22is%22, %22sunny%22, %22the%22, %22the%22, %22the%22, %22sunny%22, %22is%22, %22is%22%5D, k = 4%0A    Output: %5B%22the%22, %22is%22, %22sunny%22, %22day%22%5D%0A%0AExplanation: %22the%22, %22is%22, %22sunny%22 and %22day%22 are the four most frequent words,%0Awith the number of occurrence being 4, 3, 2 and 1 respectively.%0A%0ANote:%0AYou may assume k is always valid, 1 %E2%89%A4 k %E2%89%A4 number of unique elements.%0AInput words contain only lowercase letters.%0A%0AFollow up:%0ATry to solve it in O(n log k) time and O(n) extra space.%0A%22%22%22%0Aclass Solution(object):%0A    def topKFrequent(self, words, k):%0A        %22%22%22%0A        :type words: List%5Bstr%5D%0A        :type k: int%0A        :rtype: List%5Bstr%5D%0A        %22%22%22%0A        if not words or not k:%0A            return %5B%5D%0A        if len(words) %3C k:%0A            return %5B%5D%0A        count = %7B%7D%0A        for word in words:%0A            count%5Bword%5D = count.get(word, 0) + 1%0A        freq = %5B%5D%0A        heapq.heapify(freq)%0A        for w, c in count.items():%0A            heapq.heappush(freq, (-c, w))%0A        soln = %5B%5D%0A        for i in range(k):%0A            soln.append(heapq.heappop(freq)%5B1%5D)%0A        return soln%0A

add 692