Top K Frequent Elements | Leetcode

Поділитися
Вставка
  • Опубліковано 27 січ 2021
  • This video explains a very important programming interview problem which is to find the top k frequent elements in a given array.This problem is based on heap and hashmap.I have shown all the required concepts by showing the intuition using examples.I have first explained the simple solution and then optimized to give a better solution.I have explained the problem in two different ways using heap and hash and i have also compared the efficiency with each other and showed which is better and why.
    CODE LINK is present below as usual. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)
    ==========================================PLEASE DONATE=============================
    🧡 SUPPORT OUR WORK: / techdose
    💚 UPI-ID: surya.kahar@ybl
    💞JOIN Membership: / @techdose4u
    ==============================================================================
    INSTAGRAM : / surya.pratap.k
    LinkedIn: / surya-pratap-kahar-47b...
    WEBSITE: techdose.co.in/
    TELEGRAM Channel LINK: t.me/codewithTECHDOSE
    TELEGRAM Group LINK: t.me/joinchat/SRVOIxWR4sRIVv5...
    =======================================================================
    USEFUL LINKS:
    🟠Must do TIPS to ACE Virtual Interview: • 🔴Must do Tips to ACE y...
    🟢Best strategy to excel your coding interview: • 🔴Best strategy to exce...
    🟡Get your dream job in 1 month: • 🔴Get your dream job in...
    🔵How to crack dream job in just 2 months: • How to crack dream job...
    🟣7 Days DSA plan: techdose.co.in/7-days-dsa-che...
    RELATED LINKS:
    Power of Heap: • Power of Heap
    Concepts of Heap: • Concepts of Heap | Und...
    Representation of Heap: • Representation of Heap...
    Heapify Algorithm: • Heapify Algorithm | Ma...
    Build heap algorithm: • Build Heap Algorithm |...
    Heap Algorithm: • Heap Algorithms | Extr...
    Heapsort Algorithm: • Heapsort Algorithm | C...
    Heap Implementation: • Heap Implementation | ...
    CODE LINK: techdose.co.in/top-k-frequent...
    #heap #techdose #heapcourse

КОМЕНТАРІ • 62

  • @amanvijayvargiya3468
    @amanvijayvargiya3468 3 роки тому +72

    Finally I am placed in delhivery company with decent package ...Thanks uuh so much .You inspired me a lot .I will always remember you.

  • @ankitbatra9832
    @ankitbatra9832 3 роки тому +23

    Finally, got to see the man behind all these great videos! Thank you so much for contributing! :)

  • @AlexZaharia24
    @AlexZaharia24 2 роки тому +6

    You assume that adding elements to the heap is O(K) or O(N) which in fact will be O(KlogK) and O(NlogN) respectively. That means that the second heap algo is better than the first one.

  • @neerajpandey7273
    @neerajpandey7273 3 роки тому +10

    The explanation to choose between first (KlogN) and second(NlogK) was super.

    • @techdose4u
      @techdose4u  3 роки тому +1

      Thanks :)

    • @ShubhamKumar-lg2st
      @ShubhamKumar-lg2st Місяць тому

      he has assumed to add elements to the heap is O(K) or O(N) which in fact will be O(KlogK) and O(NlogN) respectively. That means that the second heap algo is better than the first one.

  • @anikkhan8811
    @anikkhan8811 3 роки тому +15

    Your Time Complexity analysis is top-notch!!! 😍😍 Plz continue explaining time complexity in your future videos too.

    • @techdose4u
      @techdose4u  3 роки тому +5

      Improved after so many videos 😅

    • @anikkhan8811
      @anikkhan8811 3 роки тому +2

      @@techdose4u Your experience is our quick lesson. Thanks for sharing 😀

    • @techdose4u
      @techdose4u  3 роки тому +2

      @@anikkhan8811 😂 Lol. Thanks

    • @AryanSingh-zv5ch
      @AryanSingh-zv5ch Рік тому

      @@techdose4u how come building a max heap is taking O(N) wouldn't it be O(N*logN)

  • @tulika2863
    @tulika2863 3 роки тому +3

    If we use a max heap, will it not be 0(N + KlogN)? N for heapify and KLogN for popping K times.

  • @MalladiSankar
    @MalladiSankar 2 роки тому +2

    I think we should use minheap isn't it? we need to pop from the root all the smaller values(frequencies with small values first) and add high frequency ones to the heap

  • @darshpreetnarula3034
    @darshpreetnarula3034 Рік тому +2

    No youtube video
    has explained it better than you.Thanks!

  • @shruti4879
    @shruti4879 2 роки тому

    What do you use for drawing on the canvas?

  • @rahulbera454
    @rahulbera454 2 роки тому

    Thanks for the amazing explanation

  • @hillarioushollywood4267
    @hillarioushollywood4267 2 роки тому +1

    The question arises that how to make heapify based on values and not on keys? Because, The heapify() on tuples considers the first element in the tuple for the process

  • @ommapari
    @ommapari 2 роки тому

    I love you, sir, I became a fan of your explanations... I really want to meet you someday 😢😢

  • @Hrit
    @Hrit 2 роки тому

    Most detailed explanation

  • @jaagrataggarwal4682
    @jaagrataggarwal4682 2 роки тому +3

    will heap not take O(nlogn) for insertion? You said to insert n elements ,heap would take O(n).But for rearranging after every insertion it takes logn time. So would not it take O(nlogn) time for inserting n elements?

  • @pottalokesh2039
    @pottalokesh2039 3 роки тому +3

    Also can you explain something about that comparator function in next video ...

  • @sharmabrothers3708
    @sharmabrothers3708 3 роки тому

    Sir how to implement that last approach because after making how we remember which one is pop... Because poping only from top... Please sir give light on it.
    Ret your videos is awesome sir😍

  • @nayanjain5761
    @nayanjain5761 3 роки тому +5

    Sir, please correct me if I am wrong, for build heap (line 26-29) it's going to take O(nLog(n)) time as for all n elements you are pushing into a queue (pushing is a Log(n) operation). So O(nLog(K)) will be best approach to use here.

    • @ApoorvaRajBhadani
      @ApoorvaRajBhadani 2 роки тому

      yes same doubt

    • @shivamkeshri1131
      @shivamkeshri1131 2 роки тому +3

      Build heap takes O(n) time

    • @dakshtaneja9189
      @dakshtaneja9189 Рік тому

      yes, same doubt. each push operation will take log(n) and traversing through n elements would give us nlog(n).

    • @nirnitabiswas933
      @nirnitabiswas933 6 місяців тому

      same doubt @tech-dose, from your prev videos i know build heap takes o(n) time, but pushing operation of STL PQ takes log n as it sorts also after pushing, it does that for n elements do shouldn't it be O(nlogn) ??

  • @xl3931
    @xl3931 2 роки тому

    how can I implement this solution in terms of stop words. I need to get input from a txt file and store them in data structure after that while getting top 10 frequent element of txt file, I should not count stop words. ( there is file called stopwords.txt)

    • @xl3931
      @xl3931 2 роки тому

      without using vectors

  • @The_Shubham_Soni
    @The_Shubham_Soni Рік тому

    Nice Explanation💯

  • @samareshms4591
    @samareshms4591 2 роки тому

    After storing in unordered_map why cant we iterate and push those elements into vector which are >= k

  • @princeanthony8525
    @princeanthony8525 2 роки тому +2

    The interviewer at Google expected a bucket sort solution in my case. He didn’t accept using a heap.

    • @shahenechaouachi4959
      @shahenechaouachi4959 2 роки тому +1

      Damn did u get it? It's almost impossible to come up with bucket sort on the spot if u havent seen it before

  • @SiddharthKulkarniN
    @SiddharthKulkarniN 7 місяців тому

    There is also a O(N) solution using bucket sort, isn't it?

  • @priytoshtripathi1571
    @priytoshtripathi1571 2 місяці тому

    A better solution would be to use Bucket sort technique, after counting the frequencies.We'll be able to solve it O(n)

  • @prasannapm3220
    @prasannapm3220 2 роки тому

    thank you

  • @MahirHasancse21
    @MahirHasancse21 3 роки тому +2

    Why do you use a.freqb.freq?

    • @techdose4u
      @techdose4u  3 роки тому

      Both are used for reverse order arrangement. Try it in your code. You will understand.

    • @srijandasbiswas4864
      @srijandasbiswas4864 3 роки тому +1

      So comparator in priority queue is used for reverse order arrangement !!??
      Please somebody confirm it !!

    • @ADNANAHMED-eo5xx
      @ADNANAHMED-eo5xx 3 роки тому

      @@srijandasbiswas4864 yes plz

  • @Khabib901
    @Khabib901 Рік тому

    thanks man

  • @nellylopez5152
    @nellylopez5152 3 роки тому +1

    what does while(k - -) do ?

    • @techdose4u
      @techdose4u  3 роки тому

      It will run until N becomes 0

  • @rishikantpatel5008
    @rishikantpatel5008 2 роки тому

    Hello bhaiya but to build a heap of size it will take a nlogn time?

    • @parul15137
      @parul15137 4 місяці тому

      u can push all the values in a vector first and then use build heap on it so the time taken wil be O(2n)

  • @seans9096
    @seans9096 3 роки тому +1

    how come build heap is O(N)?

    • @techdose4u
      @techdose4u  3 роки тому +1

      Watch my video on build heap algo

  • @vasanthkumar5908
    @vasanthkumar5908 Рік тому

    Build heap is N log N times because of heapify up

  • @gauravkapadia2514
    @gauravkapadia2514 3 роки тому +1

    why do we need sorting after hash map? we can just traverse one time for hash map values and filter all values whose frequency is >= k and append key to result list.

    • @shivamkeshri1131
      @shivamkeshri1131 2 роки тому

      How do you know the frequency w/o looking at the test cases?

  • @TheGameVentureYt
    @TheGameVentureYt 2 роки тому +1

    This Question was asked by Facebook.

    • @techdose4u
      @techdose4u  2 роки тому +1

      :)

    • @marin1419
      @marin1419 10 місяців тому

      does amazon ask this?@@techdose4u