• 46.8%

https://leetcode.com/problems/top-k-frequent-elements/#/description

Given a non-empty array of integers, return the k most frequent elements.

Note:

• You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
• Your algorithm’s time complexity must be better than O(n log n), where n is the array’s size.

40ms, , 20/20, May.3rd, 2016

https://leetcode.com/discuss/100562/o-log-k-unordered_map-and-priority_queue-maxheap-solution

C++ O(n log(n-k)) unordered_map and priority_queue(maxheap) solution

https://discuss.leetcode.com/topic/44337/simple-c-solution-using-hash-table-and-bucket-sort

Simple C++ solution using hash table and bucket sort

https://discuss.leetcode.com/topic/44313/3-ways-to-solve-this-problem

3 ways to solve this problem

using heap

using selection algorithm

using bucket sort

Mine Solution:

92ms, , May.3rd, 2016

Solution 1:

https://leetcode.com/discuss/100553/1-line-python-solution-using-counter-most_common-method

https://discuss.leetcode.com/topic/44237/java-o-n-solution-bucket-sort

Java O(n) Solution - Bucket Sort

Idea is simple. Build a array of list to be buckets with length 1 to sort.

https://discuss.leetcode.com/topic/48158/3-java-solution-using-array-maxheap-treemap

3 Java Solution using Array, MaxHeap, TreeMap

use an array to save numbers into different bucket whose index is the frequency

use maxHeap. Put entry into maxHeap so we can always poll a number with largest frequency

use treeMap. Use freqncy as the key so we can get all freqencies in order