Top K Frequent Elements | Heap | Bucket Sort | AMAZON | Leetcode 347 | Explanation | Live Coding

แชร์
ฝัง
  • เผยแพร่เมื่อ 15 ธ.ค. 2024

ความคิดเห็น • 63

  • @oqant0424
    @oqant0424 ปีที่แล้ว +11

    #5:58 at this point i understood why min heap is used.............earlier i solved this via max heap..........and i was confused which one to use

  • @udayshankar-e6v
    @udayshankar-e6v 9 หลายเดือนก่อน +8

    Your explantion is very clear, after Striver i only follow you.

    • @udayshankar-e6v
      @udayshankar-e6v 9 หลายเดือนก่อน +1

      please tell me what tool are you using from these drawing, i want to practice on my ipad

  • @user13443fg
    @user13443fg 3 หลายเดือนก่อน +3

    Thanks alot! Even after watching some resources about bucket sort, I was unable to wrap up my mind around it but you made it easy for me and finally solved daily problem on leetcode with bucket sort as you suggested to do at the end of your video.

  • @JJ-tp2dd
    @JJ-tp2dd ปีที่แล้ว +13

    bhai if you want to make the dp series a paid one, I'll be happy to join. I felt really bad yesterday after seeing your shorts. If it helps you and the channel in any way, please make the dp series a paid course. You deserve more subscribers and all the love ❤💙

    • @souravjoshi2293
      @souravjoshi2293 ปีที่แล้ว +4

      I am in too. This guys never even asks us to like , share or subscribe.
      He deserves it

    • @wearevacationuncoverers
      @wearevacationuncoverers ปีที่แล้ว +2

      That's true. He deserves. I don't know how he manages full time job and making these detailed videos

    • @romanraihan9133
      @romanraihan9133 ปีที่แล้ว +1

      I agree also. He deserves more...

    • @thefinalfit
      @thefinalfit ปีที่แล้ว +1

      So true. He deserves it

    • @thekindspill
      @thekindspill ปีที่แล้ว +1

      True

  • @ganeshjaggineni4097
    @ganeshjaggineni4097 12 วันที่ผ่านมา +1

    NICE SUPER EXCELLENT MOTIVATED

  • @BiswajitRout-dl5ry
    @BiswajitRout-dl5ry 7 หลายเดือนก่อน +2

    damn the second approach is very unique i solve lot of question but i am not able think this way i learned the new technique (bucket sort) thanks bhiya ....

  • @technologicalvivek7510
    @technologicalvivek7510 ปีที่แล้ว +1

    bhaiya aap samjate bahut aacha ho.Tough ko bhi easy kar dete ho.Thank you for this lecture.

  • @somnathdas8470
    @somnathdas8470 5 หลายเดือนก่อน +1

    cristal clear explanation

  • @JJ-tp2dd
    @JJ-tp2dd ปีที่แล้ว +2

    Java Solution:
    Priority Queue:
    class Pair{
    int first;
    int second;
    public Pair(int first,int second){

    this.first = first;
    this.second = second;
    }
    }
    class Solution {
    public int[] topKFrequent(int[] nums, int k) {

    Map mp = new HashMap();
    for(int num : nums) {
    mp.put(num, mp.getOrDefault(num,0)+1);
    }
    PriorityQueue pq = new PriorityQueue((x,y) -> x.first - y.first);

    for(var entry : mp.entrySet()) {
    int value = entry.getKey();
    int freq = entry.getValue();

    pq.offer(new Pair(freq,value));

    if(pq.size() > k){
    pq.poll();
    }
    }

    int[] result = new int[k];
    for(int i=0; i= 0; i--){
    if(bucket[i] != null){
    for(int v : bucket[i]){
    res[index++] = v;
    if(index == k) return res;
    }
    }
    }
    return res;
    }
    }

  • @sidharthdhiman4522
    @sidharthdhiman4522 ปีที่แล้ว

    great sir , maine bhi yahi socha priority quey and woh reverse map ka , reverese map wale me tle arha tha kafi time lagaya thik nhi hua and yeh priortiy queu wale me mai woh class comparator ka soch rha tha but usme bhi thik nhi hua ab apka video tdekha toh galti samjh ayi
    and thanx sir time compleixty discussion ke liye bhi yeh sabse best hai ,

  • @itssumit6161
    @itssumit6161 6 หลายเดือนก่อน +1

    very nyc explanation sir hats off

  • @ManojKrVerma-vw4dx
    @ManojKrVerma-vw4dx ปีที่แล้ว +1

    Second solution is new for me. ThankYou

  • @sehajdeepsingh7427
    @sehajdeepsingh7427 ปีที่แล้ว +2

    Great approach !!

  • @wearevacationuncoverers
    @wearevacationuncoverers ปีที่แล้ว

    I knew this guys will be adding extra more approach. Thanks for the bucket sort solution

  • @vengalrao5772
    @vengalrao5772 ปีที่แล้ว +2

    You deserve subs more bhaiyya 😢❤
    Big Fan of your hardwork and consistency.

    • @codestorywithMIK
      @codestorywithMIK  ปีที่แล้ว

      It means a lot to me. Thank you for your kind words ❤️❤️❤️

  • @arjunsolanki2612
    @arjunsolanki2612 ปีที่แล้ว +2

    Last approach was Lit🔥.

  • @tanishchordia4813
    @tanishchordia4813 10 หลายเดือนก่อน +1

    Mik , In one of previous lectures you taught how to make a customize seperator acc. to the problem so i used that still did this with max heap making lambda as my custom operator
    thank you🎉

    • @codestorywithMIK
      @codestorywithMIK  10 หลายเดือนก่อน +1

      That’s awesome ❤️❤️❤️

  • @10minutes_cs
    @10minutes_cs 6 หลายเดือนก่อน +1

    14:03 ::: approach 2 (Bucket sort)

  • @anuppatankar4294
    @anuppatankar4294 ปีที่แล้ว +1

    Great Video ❤

  • @susantamandal2782
    @susantamandal2782 3 หลายเดือนก่อน

    Hi MIK, thank you for the details explanations for all the different problems. I am now one of your follower :)
    I have a small doubts, at time 13:29, the PriorityQueue add operations will happen n times or k times. because irrespective of the k, we are adding all the items.

  • @ajit287
    @ajit287 ปีที่แล้ว +1

    Thank You bro. One request please make a detail video on comparator in c++

  • @alokgautam02
    @alokgautam02 ปีที่แล้ว +1

    Thanks for this detailed soln.😇

  • @shreyatiwari8239
    @shreyatiwari8239 4 หลายเดือนก่อน

    best channel

  • @shashwatchawla
    @shashwatchawla ปีที่แล้ว +3

    // Brute force solution
    class Solution {
    public:
    static bool cmp( const pair& a, const pair& b) {
    return a.second > b.second;
    }
    vector topKFrequent(vector& nums, int k) {

    mapmp;
    for(auto x:nums)
    {
    mp[x]++;
    }
    vectorv(mp.begin() , mp.end());

    sort(v.begin(), v.end() ,cmp );
    vectorans;

    for(int i=0;i

  • @chad._life
    @chad._life 8 หลายเดือนก่อน

    thankyou for kind help
    we can solve it in quite simple way also this is how
    C++
    class Solution {
    public:
    vector topKFrequent(vector& nums, int k) {
    unordered_mapmp;
    vectorans;
    priority_queuepq;
    for(auto i:nums){
    mp[i]++;
    }
    for(auto &it:mp){
    pq.push({it.second,it.first});
    }
    while(k--){
    int temp=pq.top().second;
    pq.pop();
    ans.push_back(temp);
    }
    return ans;
    }
    };

  • @udaykumarchetla6205
    @udaykumarchetla6205 ปีที่แล้ว

    Bro ur there on codeforces and codechef?

  • @DivVj
    @DivVj ปีที่แล้ว

    Can you make the playlist specific to the companies? Like for questions which was asked in Microsoft would be gathered in one playlist If possible.

  • @oqant0424
    @oqant0424 ปีที่แล้ว

    thanks bhaiya

  • @udaykumarchetla6205
    @udaykumarchetla6205 ปีที่แล้ว +2

    Mik, please use a mic. Voice is low without head phones.

    • @codestorywithMIK
      @codestorywithMIK  ปีที่แล้ว +1

      Will soon buy a mic for fixing this issue.
      Thanks a lot for the feedback

  • @rounaq_khandelwal
    @rounaq_khandelwal ปีที่แล้ว

    ughh, incase of higher size of the nums array, soln 2 will fail,right(since it will lead to memory limit exceeded)?

    • @PrashantSingh-sp4ti
      @PrashantSingh-sp4ti 11 หลายเดือนก่อน

      No , it wont max value is just 10000 so will be the frequency if all digits are same

  • @amankunwar8621
    @amankunwar8621 หลายเดือนก่อน

    make a video of k closet point to origin

  • @coldcoke9254
    @coldcoke9254 3 หลายเดือนก่อน

    bucket sort mei quadratic time complexity kyu nhi hai koi explain kr skta hai mujhe. mai humesha aisi questioni mei atakta hu

  • @romanraihan9133
    @romanraihan9133 ปีที่แล้ว

    16:40 If we sort that, it will be a massacre. the track of the index will be lost. ( just sharing an insight for a noob like me).

  • @technologicalvivek7510
    @technologicalvivek7510 ปีที่แล้ว

    Bro I need a help. I have written a code using map but it passes some test case.Please help me to find out what are the mistake I do in this code.
    class Solution {
    public:
    vector topKFrequent(vector& nums, int k) {
    mapmpp;
    vectorans;
    for(int i=0;i=k)
    {
    ans.push_back(it.first);
    }
    }
    return ans;
    }
    };

    • @sehajdeepsingh7427
      @sehajdeepsingh7427 ปีที่แล้ว

      why are you checking the freq of the element with k?
      k is just no. of terms required. and it.second is the freq of each element that appeared in nums.
      Instead make a max-heap that stores all the freq along with the value, and extract top k elements into your answer vector.

  • @molyoxide8358
    @molyoxide8358 ปีที่แล้ว +1

    Bhai aapka toh mail bhar jaata hoga YT comments se

    • @codestorywithMIK
      @codestorywithMIK  ปีที่แล้ว

      Yes 😅

    • @30sunique78
      @30sunique78 5 หลายเดือนก่อน +1

      This compliment from my side ❤ Thanks man​@@codestorywithMIK

  • @ezcoding69
    @ezcoding69 ปีที่แล้ว

    CAN ANYONE TELL ME THE TC OF THIS CODE ?
    HashMapmap=new HashMap();
    if(nums.length == k) return nums;
    for(int i=0;imap.get(a)-map.get(b));

    int res[]=new int[k];
    for(int z:map.keySet()){
    pq.add(z);
    if(pq.size()>k)pq.poll();
    }
    int x=0;
    while(!pq.isEmpty())res[x++]=pq.poll();


    return res;
    Is it also O(NLOGK) ??