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():
            buckets[num_appearances].append(x)
            
        res = []    
        for i in range(len(buckets) - 1, 0, -1):
            for x in buckets[i]:
                res.append(x)
                if (len(res) == k):
                    return res;


Comments

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

Functional Programming in Scala for Working Class OOP Java Programmers - Part 1

Introduction Have you ever been to a scala conf and told yourself "I have no idea what this guy talks about?" did you look nervously around and see all people smiling saying "yeah that's obvious " only to get you even more nervous? . If so this post is for you, otherwise just skip it, you already know fp in scala ;) This post is optimistic, although I'm going to say functional programming in scala is not easy, our target is to understand it, so bare with me. Let's face the truth functional programmin in scala is difficult if is difficult if you are just another working class programmer coming mainly from java background. If you came from haskell background then hell it's easy. If you come from heavy math background then hell yes it's easy. But if you are a standard working class java backend engineer with previous OOP design background then hell yeah it's difficult. Scala and Design Patterns An interesting point of view on scala, is