Leetcode 347 - Top K Frequent Elements

Поделиться
HTML-код
  • Опубликовано: 26 окт 2024

Комментарии • 28

  • @GregHogg
    @GregHogg  Месяц назад +6

    Prepare for coding interviews for FREE at AlgoMap.io!

  • @Mongovi
    @Mongovi 27 дней назад +13

    I appreciate this series of shorts, after watching a few of these, I decided I'm better off in UI/UX and graphic design. Thank you!

  • @TSalama7
    @TSalama7 7 дней назад +3

    There’s also an O(n) solution using counting sort on an array of size n where the indices are counts and the values are lists of the elements with those counts; then append from the highest to lowest counts till we hit k

  • @HungryBen-d7y
    @HungryBen-d7y Месяц назад +1

    Loop through the keys and look for values of the keys that are equal to the k element then return [1,2]

  • @vatsalsharma4061
    @vatsalsharma4061 Месяц назад +15

    first solution i got correct

  • @aniketmore8216
    @aniketmore8216 Месяц назад +1

    In python we can use coutner to return the count of each element and then comvert that into list sort it and print last two index

    • @MilesIsReal
      @MilesIsReal Месяц назад

      That’s less efficient. You need to loop through every time for every unique element in that solution rather than one loop where you manually count

  • @דניאלאביב-ו6ת
    @דניאלאביב-ו6ת Месяц назад +6

    Why not max heap it is more efficient in terms of complexity?

  • @yashwanthnadella537
    @yashwanthnadella537 Месяц назад +3

    Shouldn't the time complexity be k log n?

  • @ujjalnandi1542
    @ujjalnandi1542 21 день назад

    Why cant we just iteratethe map entry set and return key for value 2. Time Complexity will be O(2n)

  • @kamilmensik6521
    @kamilmensik6521 7 дней назад

    Hello today we are going to solve a problem:
    First off we allocate memory.
    Second off: we allocate even more memory.
    Yay!

  • @User948Z7Z-w7n
    @User948Z7Z-w7n 23 дня назад

    Why not just sort since using heap is basically a form of sorting and the time complexity is nlogn anyways

  • @pravyn350
    @pravyn350 29 дней назад

    It’s just counter ?

  • @guedjs
    @guedjs 28 дней назад +1

    you didn't give the complexity of the optimal solution :(

  • @Milei1012
    @Milei1012 Месяц назад +10

    With how useful hashmaps are i think that it shouldnt be allowed. It makes everything hash worthy

    • @s1l3nttt
      @s1l3nttt Месяц назад +2

      never used a hashmap, I do real programming instead

  • @hafizhbloody1455
    @hafizhbloody1455 Месяц назад +1

    nlogk?

    • @yashwanthnadella537
      @yashwanthnadella537 Месяц назад

      Yeah, I thought it was klogn

    • @MohammedAli-pb1fg
      @MohammedAli-pb1fg Месяц назад

      No O(n) ..cause inserting ,updating any element in hash map take 0(1) time...total space will be O(no. Of distinct number in arrays)

    • @philstanton8912
      @philstanton8912 24 дня назад

      @@MohammedAli-pb1fgthere a few misconceptions here.
      If you consider hashmap insertion to be O(1), then inserting everything into the map is O(n). However, the process of iterating over each map pair and inserting into the heap is O(nlog(k)). Its not hard to see that n < nlog(k). Therefore, O(nlog(k)) dominates and is the correct runtime under these assumptions.
      2nd, hashmap insertion is actually O(n) and not O(1) . So the algorithm is technically O(n^2).

  • @MsWorldMine
    @MsWorldMine Месяц назад +5

    These stuff is so useless to learn I can’t believe they are still around