This video was amazing and really helped me in preparing for technical interviews! thank you so much for this, you just got a new subscriber :D
@AlgosWithMichael2 жыл бұрын
Thank you for the support!
@Metachief_X2 жыл бұрын
Hey bro, god bless you. i was struggling with this problem for a couple hours.... studying hard for my next interviews... you rock! keep making videos :)
@darthvader_3 жыл бұрын
Michael, you are a savior! I happened to solve this question earlier but the explanation you gave helped a lot!
@CostaKazistov2 жыл бұрын
Nice walkthrough and the solution. Here is Kotlin version: class Solution { fun topKFrequent(words: Array, k: Int): List { val result = mutableListOf() val comparator = Comparator { a, b -> if (a.value != b.value) b.value.compareTo(a.value) else a.key.compareTo(b.key) } val queue = PriorityQueue(comparator) words.groupingBy { it }.eachCount().forEach { queue.offer(it) } while (result.size < k && queue.isNotEmpty()) result.add(queue.poll().key) return result } }
@snowy-ur9qq Жыл бұрын
Thanks for the clear explanation! I have a follow-up question about time complexity - since each of the n entries in the map is added to the queue (and polled only if the queue size exceeds k), shouldn't the time complexity be O(n*logn)?
@animatedzombie643 жыл бұрын
N time for map insertion, Nlogk for actual heap insertion because it heapifies on every insertion, and finally Klogk for reversing the list. so N + Nlogk + Klogk => NlogK upperbound.
@Bubdiddly2 жыл бұрын
Thank you this is what I was wondering
@manojrajasekar60354 жыл бұрын
Fantastic one Michael. Keep doin more :)
@AlgosWithMichael4 жыл бұрын
Definitely, thank you for watching!
@silverbullet_63 жыл бұрын
Thanks bro i Almost forgot about the @override method.❤️
@AlgosWithMichael3 жыл бұрын
No problem 👍
@amberlala21634 жыл бұрын
I think that using lambda expression to initialize the priority queue is more concise. ^-^
@AlgosWithMichael4 жыл бұрын
Very true, I just didn't want to use that way in case people had not seen lambda syntax before.
@z41n703 жыл бұрын
For those wondering how to do the lamba version: PriorityQueue pq = new PriorityQueue((a, b)->map.get(a)-map.get(b)==0?b.compareTo(a):map.get(a)-map.get(b));
@KanagaveluSugumar2 жыл бұрын
I dont think we need to reverse it. Need to change word1.compareTo(word2) ? By the way your videos are more helpful. Thank you!
@manjunathbhadrannavar51652 ай бұрын
My que is, why use a priority queue? because we just need to return top k from a map of key value pairs, sort using a custom comparator and return top k values, simple 🤷♂
@GochiCity Жыл бұрын
So the reason we create a min heap for getting MAX k values is to reduce the amount of elements we store in the heap. If we do a max heap then we will continue to keep storing values we are not going to use. Interesting
@KCML0362 жыл бұрын
how come we don't take comparing the string lexicographically part into the Big O? Wouldn't that be worst case length of the maximum string. Say max string length is m then wouldn't the big O be n*(m*logk)
@sofiayz74723 жыл бұрын
I know it's going to be a very dumb question but why frequency1-frequency2?
@AlgosWithMichael3 жыл бұрын
Not a dumb question at all! It is because we need to sort the lesser frequencies first. So for example, lets say frequency1 is 2 and frequency2 is 4, this would do 2 - 4 = -2. Since it is a negative number, we are saying word1 is less than word2 in sorted order.
@sofiayz74723 жыл бұрын
@@AlgosWithMichael Thank you so much for your reply! You are absolutely a great teacher!I just subscribed to your channel and can't wait to learn more from you!
@AlgosWithMichael3 жыл бұрын
No problem! Thank you so much for watching my content, I really appreciate it 👍
@oven59972 жыл бұрын
Thanks for sharing, great explanation
@AlgosWithMichael2 жыл бұрын
My pleasure!
@franklinshih91693 жыл бұрын
great explanation omg thank you
@wendycheng68563 жыл бұрын
Great explanation! Pretty Clear!
@AlgosWithMichael3 жыл бұрын
Glad it was helpful!
@AnuragBaidyanath4 жыл бұрын
Great explanation michael!
@AlgosWithMichael4 жыл бұрын
Glad it was helpful!
@VinodKumar-vd9ou2 жыл бұрын
Bro, nice video but i think storing words in Map is not a good idea in case we have huge number of string files, Trie could be a better approach for efficient storage and also you can store counts at leaf node of trie.
@priyanshumohanty5261 Жыл бұрын
I think based on the constraints specified in the question, this sure is a good method. Besides, tries can be trickier
@tomladdus92644 жыл бұрын
What would you do if your standard library does not have a Priority Queue? For example, Swift, if this is asked by Apple, does not have a PQ as part of the standard library. Would they expect you to write one on the spot?
@AlgosWithMichael4 жыл бұрын
I think what they would do is just say to assume you have a Priority Queue implementation already written
@ArghyaBhattacharyaNITA3 жыл бұрын
"Love isn't supposed to come before 'I" '. That my friend, is a heavy truth :)
@sumitrakumarishaw28853 жыл бұрын
Thanks from India
@indiansoftwareengineer48993 жыл бұрын
Thanks Michael.
@AlgosWithMichael3 жыл бұрын
Thank you!
@sputnikone37243 жыл бұрын
What is the purpose of using a minheap here? Why not use a maxheap instead and then pop the top K ones?
@chiragchandak95544 жыл бұрын
amazing work brother
@AlgosWithMichael4 жыл бұрын
Thank you so much 😀
@Руслан-х7и2э Жыл бұрын
thank you man, i understand)
@codeduel2 жыл бұрын
The last return of the Comparator isn’t safe. The code may give the wrong answer when there is overflow. For instance, when comparing a large negative value to a large positive value, the difference may be more than the largest value that can be stored in an integer, Integer.MAX_VALUE
@mohammadyahya782 жыл бұрын
Thank you very much. I still don't understand the comparator and why you said frq1-freq2 while word2.compareTo(word1) and not word1.compareTo(word2) please?
@SheikhSadiruddin12 жыл бұрын
You want to remove the less frequent words and those that have greater alphabetical ordering.
@bz90314 жыл бұрын
Hi, I have a question. Why do we have to use map.Entry in this case? Could we simply use map.keySet()?
@AlgosWithMichael4 жыл бұрын
You could also use a keySet 😀
@BlumChoi Жыл бұрын
if k=1 and I have n unique strings, than the sorting suggested in the video will take O(nlog(n)), wouldn't it?
@codeduel Жыл бұрын
No. It will be O(n)
@christeenathomas61083 жыл бұрын
please solve this problem using python3
@AlgosWithMichael3 жыл бұрын
Yea, if enough people want python instead of java I can do that
@venkataraman27162 жыл бұрын
do you have python 3 code using same approach
@leomonz4 жыл бұрын
Why can’t you do word1.compareTo(word2)?
@AlgosWithMichael4 жыл бұрын
Because you only sort by the word strings if the frequencies are the same
@obaid57614 жыл бұрын
@@AlgosWithMichael i think he meant why it's the other way around
@satheshbm924 жыл бұрын
Yes I have the same question. Why can't we use word1.compareTo(word2) in the comparator?
@AlgosWithMichael4 жыл бұрын
Oh I see. You still can't flip them, you would get different results if you did word1.compareTo(word2). I just tried now and failed multiple test cases :/
@uvedkhan14 жыл бұрын
@@AlgosWithMichael Thats the question, why is like that?
@woodylucas3 жыл бұрын
Is that comparator class similar to JavaScript sort method?
@AlgosWithMichael3 жыл бұрын
Yes, I believe so
@jeffreylee9114 жыл бұрын
why don't we do this in correct order directly instead of reversing it in the end?
@AlgosWithMichael3 жыл бұрын
2 months late on this response lol sorry did not see it. The reason why is because when we insert into our priority queue, we are inserting the words in reverse alphabetical order, we want it in alphabetical order for the final result
@jeffreylee9113 жыл бұрын
@@AlgosWithMichael But still helpful! Thanks a lot!
@namisha68833 жыл бұрын
@@AlgosWithMichael Why are we not counting complexity of reverse sorting in the end, which is O(N Log N) and certainly greater than O(N* log k)
@Alex7nt3 жыл бұрын
@@namisha6883 reverse is not NlogN, you do N/2 operations to reverse cause u swap a[i] with a[length - i]. And since N/2 < NLOGK the timr complexity mantains in nlogk
@soulboy22233 жыл бұрын
perfect!
@AlgosWithMichael3 жыл бұрын
thanks!
@NguyenCuong-sg2kg3 жыл бұрын
How about use Trie
@AlgosWithMichael2 жыл бұрын
I'm not sure if a Trie would improve the complexity
@pramodsharma45083 жыл бұрын
The problem was asked at a google interview with input size of 1 trillion
@AlgosWithMichael3 жыл бұрын
Oof Google be asking hard questions :p
@VadimFilin2 жыл бұрын
Lol, implement PriorityQueue in C or Go at first. Dislike