This refers to leetcode problem: https://leetcode.com/problems/top-k-frequent-words/ Here is my code:
import heapq
class Solution:
# def topKFrequent(self, words: List[str], k: int) -> List[str]:
def topKFrequent(self, words, k):
results = []
wordTable = {}
for word in words:
if (wordTable.get(word) is None):
wordTable[word] = 1
continue
wordTable[word] = (wordTable.get(word)) + 1
heap = []
# print(wordTable)
heapSize = 0
for word in wordTable.keys():
node = [wordTable[word], word]
if(heapSize<k):
heapq.heappush(heap,node)
heapSize += 1
continue
if(heapSize>=k):
if (heap[0][0]< node[0]):
heapq.heappushpop(heap,node)
heapSize -= 1
continue
if heap[0][0] == node[0] and heap[0][1]>node[1]:
heapq.heappop(heap)
heapq.heappush(heap,node)
heapSize -= 1
continue
# heap.sort(key = lambda x: x.freq, reverse=True);
print(heap)
for i in reversed(range(k)):
results.append(heap[i][1])
return results
The code works if all the words have different frequency, since it uses a min-heap. However, it doesn't work if they have the same frequency since it is going in reverse order, so the word that is alphabetically greater comes first which is not accepted (for example, if I have 4 words with same frequency and lets say they are a,b,c,d: my result will be d,c,b,a which is not acceptable) I am not sure how to account for this case and am stuck for 3 hours on this issue. Can anyone please help?