Skip to main content

LeetCode 347. Top K Frequent Elements

Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order.

Example 1:

Input: nums = [1,1,1,2,2,3], k = 2
Output: [1,2]

Example 2:

Input: nums = [1], k = 1
Output: [1]

Option 1 - Map to count + Sort to get top k - O(nlgn)

  1. We need to count what is the freq or how many times an item appeared.
  2. The obvious thing is for each item put it in a hashmap where
    1. Key is the item
    2. Value is the number of times it appeared
  3. Now the only thing that is left is to sort this hashtable.
  4. sorting is O(nlgn), placing everyting and counting is O(n) so this makes it overall O(nlgn)

Option 2 - Improvement over option 1 - Instead of sorting use a Heap

  1. This is a common thing when you need to sort but the core of the problem is not sorting but as in this example only get top k then we could utilize a heap.
    1. We still build a map as in option 1 which takes O(n) this is the counting map.
    2. Building a heap heapify takes O(n)
    3. Remove each item from the heap is O(lg(n)) until we removed k items - as each pop from a heap is lg(n)
    4. Overall this makes it klg(n) + n which can be better than nlgn if k < n

Option 3 - O(n) - Use buckets for finding the top k

  1. Start as usual build a hashtable for counting how many times each item appears this would be O(n)
  2. Now create an array [] of size of number of items we have here index i would be a list of the number occuring i times
    1. [].get[7] --> [the items that appeared 7 times]
  3. And as you see to get the top k we just need to traverse the top k elements of this list
  4. Overall this means O(n) for creating the hashtable and O(n) for traversing this buckets list.

Here is the actual implementation of the buckets solution:

class Solution(object):
    def topKFrequent(self, nums, k):
        :type nums: List[int]
        :type k: int
        :rtype: List[int]
        # step 1 - put it in the map 
        #         [1,1,3,4] --> {
        #                             {1 --> 2}
        #                           , {3 --> 4}
        #                           , {4 --> 1}
        #                        }
        # step 2 prepare buckets [0, ... nums.length ] -> a number can appear at most nums.
        #        Bonus - buckets already sorted by index last bucket is one with highest number if count.
        # Step 3 Read buckets in reverse order.
        appearances = {} # map {number -> # times appears }
        for x in nums:
            appearances[x] = 1 + appearances.get(x, 0)
        buckets = [[] for i in range(len(nums) + 1)] # [] - An array of arrays, we are going to keep all the x that are in each bucket appeared number of times as index of bucket.
        for (x, num_appearances) in appearances.items():
        res = []    
        for i in range(len(buckets) - 1, 0, -1):
            for x in buckets[i]:
                if (len(res) == k):
                    return res;


Popular posts from this blog

Dev OnCall Patterns

Introduction Being On-Call is not easy. So does writing software. Being On-Call is not just a magic solution, anyone who has been On-Call can tell you that, it's a stressful, you could be woken up at the middle of the night, and be undress stress, there are way's to mitigate that. White having software developers as On-Calls has its benefits, in order to preserve the benefits you should take special measurements in order to mitigate the stress and lack of sleep missing work-life balance that comes along with it. Many software developers can tell you that even if they were not being contacted the thought of being available 24/7 had its toll on them. But on the contrary a software developer who is an On-Call's gains many insights into troubleshooting, responsibility and deeper understanding of the code that he and his peers wrote. Being an On-Call all has become a natural part of software development. Please note I do not call software development software engineering b

SQL Window functions (OVER, PARTITION_BY, ...)

Introduction When you run an SQL Query you select rows, but what if you want to have a summary per multiple rows, for example you want to get the top basketball for each country, in this case we don't only group by country, but we want also to get the top player for each of the country.  This means we want to group by country and then select the first player.  In standard SQL we do this with joining with same table, but we could also use partition by and windowing functions. For each row the window function is computed across the rows that fall into the same partition as the current row.  Window functions are permitted only in the  SELECT  list and the  ORDER BY  clause of the query They are forbidden elsewhere, such as in  GROUP BY ,  HAVING  and  WHERE  clauses. This is because they logically execute after the processing of those clauses Over, Partition By So in order to do a window we need this input: - How do we want to group the data which windows do we want to have? so  def c

Building Secure and Reliable Systems

A recent book was published this year by Google about site reliability and security engineering, I would like to provide you a brief overview of it and incorporate my own analysis and thoughts about this subject while saving you some time from reading, at least part of it. Take a few of your customers and ask them, what are the top 5 features on my product that you like.  The answer that you are likely to get is, I really like how polished the UI is, or the daily report I get by mail is just fantastic, or since I started using your product I was able to save one hour a day my productivity got up and the share /chat button on document that you added recently is doing a great job. Your customers are very unlikely to answer the question of what top 5 features of my product do you like with I really like its security or I really like that we lost no chat messages since I started using it.  No real customer will even think of it, moreover, assuming you did a very good job, they won&#