Grokking the Coding Interview: Patterns for Coding Questions
Vote

0% completed

​

Solution: Top 'K' Frequent Numbers

.....

.....

.....

Like the course? Get enrolled and start learning!
A

Anonymous

· 3 years ago

We can use the same loop, where we add/update into map and also checks with the queue top element

A

Anonymous

· 4 years ago

For the python solution, is there a benefit of the code block of "while minHeap: topNumbers.append(heappop(minHeap)[1])"? Would it not be cheaper time wise to simply iterate through the tuples within minHeap and append the element to topNumbers? This way will result in only O(k) rather than O(klogk) time, which won't matter in the overall time complexity. However, I am just trying to see if there is some advantage to the first approach that I am missing.

Show 1 reply
A

Anonymous

· 4 years ago

There's an O(N) time & space solution for this question using a slightly modified version of Bucket Sort, specifically useful for this type of question (top K frequent numbers/ letters- see Bucket Sort filtered questions on Leetcode).

Its usage for optimal solutions isn't common, but it still might be useful to link to/ mention this idea.

Show 1 reply

Reading Progress

0%


Vote for new content