857. Minimum Cost to Hire K Workers | Priority Queue | Heap | Kinda Sliding Window

Поділитися
Вставка
  • Опубліковано 26 сер 2024
  • In this video, I'll talk about how to solve Leetcode 857. Minimum Cost to Hire K Workers | Priority Queue | Heap | Kinda Sliding Window
    Let's Connect:
    📱Discord (Join Community) : / discord
    📝Linkedin: / aryan-mittal-0077
    📸 Instagram: / codewitharyanbhai
    💻 Twitter - / aryan_mittal007
    🤖 Github: github.com/ary...
    About Me:
    I am Aryan Mittal - A Software Engineer in Goldman Sachs, Speaker, Creator & Educator. During my free time, I create programming education content on this channel & also how to use that to grow :)
    ✨ Hashtags ✨
    #programming #Interviews #leetcode #faang #maang #datastructures #algorithms

КОМЕНТАРІ • 68

  • @ARYANMITTAL
    @ARYANMITTAL  3 місяці тому +16

    Please do like & share with your friends or other coding communities, ifff and only if you like our videos & want us to make more such regular editorials. Your motivation to watch these videos is to get high package jobs & my motivation is your love ❤

    • @anurag_vish
      @anurag_vish 3 місяці тому

      Bhaiya you are amazing teacher...

  • @shivchauhan795
    @shivchauhan795 3 місяці тому +7

    Bechara was personal🤣. Thanku pura question ache se samajh aa gya.

  • @mrityunjoybarman9098
    @mrityunjoybarman9098 3 місяці тому +5

    Ohhh god when will I be able to solve these problems all by myself. But great explanation

  • @shikharchaturvedi5093
    @shikharchaturvedi5093 3 місяці тому +3

    One of the best explanation of a hard problem i ever saw. Thanks bro. Keep up the Good work!

  • @lavanyam3224
    @lavanyam3224 3 місяці тому +2

    The best explanation! was fun to watch how u called the lowest wage person bechara😂

  • @gursimransingh3707
    @gursimransingh3707 3 місяці тому +7

    bhayia slides v share krdea kro... helpfull raheta hai... notes bnane mai

  • @lavanyam3224
    @lavanyam3224 3 місяці тому +4

    one doubt @ 24:07 Since we're using the ratio of the new quantity, that quantity should be part of the quantitySum.. suppose the heap size > k and the new quantity is the greatest say 5 and we need to remove it.. in that case, what if we remove it from the heap & qualitySum and just use it's ratio?

    • @ARYANMITTAL
      @ARYANMITTAL  3 місяці тому +3

      That is an awesome question, firstly hats off to even think in such deep🫡
      .
      But my claim is : if quality is high of curRatio that we will have to ultimately remove from priority queue, then it will never contribute to minAns.
      .
      .
      Conclusion: thus including or excluding this ratio will never even matter for our ans
      .
      .
      Proof: Also proof is purely mathematically but i’ll explain with an example ( making this example generic )
      .
      .
      Step1- as all ratios are sorted thus number these ratios as r1, r2, r3, r4. Now as they are sorted just assign them random but sorted values. r1 = 1, r2 = 2, r3 = 3, r4 = 4 (you can take any ratio values but sorted)
      Step 2 - now we imagine we had to take 2 employees, and also imagine you are at 4th employee i.e. index = 3 (4th employee)
      Step 3 - (Assumption to prove the fact) - Now we are saying the ith employee has higher quality & we already have lesser k qualities in the heap. Okay, lets say ith employee has quality 5, while you already had in the heap quality of 2 & quality of 1 (imagine for index 1 & 2 respectively)
      Step 4 - Now as ratios are sorted in ascending order and index3 employee had a ratio of 4 (r4 = 4) and quality as 5 (just considered higher than 2 & 1), so wage which i'll be giving this 3rd index employee will be 5 * 4 = 20$
      Step 5 - While if we already had a quality of 2 (taking max quality out of 2 & 1), and we also know its ration will for sure be less than 3rd employee (assumed ratio was 2), thus his wage will be 2*2 = 4 (same will follow for second employee too)
      Thus, it proves, that as ratios are sorted in ascending order, this will make sure we take minRatio & multiply by minQualitySum to get minPaidSalary to these k employees.
      I hope this solves the doubt.

    • @abhimanyuambastha2595
      @abhimanyuambastha2595 3 місяці тому

      The simple explanation is that if we removed the person and used his ratio, that means we are using a higher ratio (since sorted) in the current iteration, but using the quality from last iteration. This will never be minimum than the previous ans and won't matter in the end

  • @dhineshd94
    @dhineshd94 3 місяці тому

    best explanation for this problem

  • @ushapandey1088
    @ushapandey1088 3 місяці тому

    Honeslty i derive the formula that u derive , but uske baad how to use it i learnt from u👍..this is my learning

  • @prathamjain8937
    @prathamjain8937 3 місяці тому

    Amazing explaination , Didn't imagine I will completely understand hard problem in 1 go.
    I know I am asking for lot but can you make a giude on how should I follow your intution building playlists becoz questions are arranged in cronological order and some questions are difficult to understand at first

  • @HIMANSHUYADAV-dt2kb
    @HIMANSHUYADAV-dt2kb 3 місяці тому

    Excellent explanation😃😃

  • @iamnoob7593
    @iamnoob7593 Місяць тому

    Brilliant Aryan.

  • @chinmoydebnath353
    @chinmoydebnath353 3 місяці тому +1

    good luck, coming up with this during interview😮‍💨

  • @navinbarnwal4782
    @navinbarnwal4782 3 місяці тому

    Nice explanation ❤

  • @manoharmanu9240
    @manoharmanu9240 3 місяці тому +1

    Great explanation 🙌

  • @vishwakarma-gaurav
    @vishwakarma-gaurav 3 місяці тому

    Amazing 🔥🔥

  • @floatingpoint7629
    @floatingpoint7629 3 місяці тому

    your explanation was really good

    • @floatingpoint7629
      @floatingpoint7629 3 місяці тому

      it would have been great if you could have dry run a complete example at the end

  • @rajrajesh1669
    @rajrajesh1669 3 місяці тому +1

    Can anyone help me with this part?
    I get it that we will find the minimum pay by sorting the ratios and maintaining a priority queue to get the min qualities, but what if that total cost that we found didn't meet the minimum wage expectation of one or more people?

    • @akshatgoel149
      @akshatgoel149 20 днів тому

      We are taking values which are behind the current index and the array is sorted based so for ith element either we will pick its own ratio or a ratio bigger than its ratio which will eventually give more than its min wage

  • @kandariarjun
    @kandariarjun 3 місяці тому

    You explained it very well. Thanks a lot.

  • @iamnoob7593
    @iamnoob7593 Місяць тому

    Request you to kindly also do LC 759 , 871.

  • @akshayawasthi599
    @akshayawasthi599 3 місяці тому

    great work brother.. hats off..

  • @Anushka20037
    @Anushka20037 2 місяці тому +1

    i don't get him after 18:00

  • @asmitshukla4649
    @asmitshukla4649 3 місяці тому

    bhaiya jhoot ni bolunga, ekdam sir k uppar se gaya ye question................LEETCODE HARD NI BANTE HAIN MUJHSE😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭

  • @ompatel2212
    @ompatel2212 3 місяці тому +1

    you should share the notes

  • @prathameshautade2679
    @prathameshautade2679 3 місяці тому

    Great Explanation, Thanks

  • @ShristiSethiya-ch2he
    @ShristiSethiya-ch2he 3 місяці тому

    Great Explanation!!

  • @yuvikasingh9635
    @yuvikasingh9635 3 місяці тому

    Excellent explanation

  • @MichaBalcerak
    @MichaBalcerak 3 місяці тому

    What should I do when I don't have such an intuition for questions like that ?

  • @prashantsahu5117
    @prashantsahu5117 3 місяці тому +4

    hello ji just search for that video got it in just NOW

    • @ARYANMITTAL
      @ARYANMITTAL  3 місяці тому +1

      Bilkul ji ❤, thank you ji 🫂

  • @mukulkhanna5071
    @mukulkhanna5071 3 місяці тому

    This dialogue holds for this awsome video "aryan bhai ke age koi bol skta hai ky....."

  • @IK-xk7ex
    @IK-xk7ex 3 місяці тому

    Thank you for explanation.

  • @abiraljain7313
    @abiraljain7313 3 місяці тому

    Great explanation 🔥🔥🤩

  • @parthgupta971
    @parthgupta971 3 місяці тому +2

    @ARYANMITTAL what happen if the i th element got removed from priority queue?

    • @ARYANMITTAL
      @ARYANMITTAL  3 місяці тому +4

      That is an awesome question, firstly hats off to even think in such deep🫡
      .
      But my claim is : if quality is high of curRatio that we will have to ultimately remove from priority queue, then it will never contribute to minAns.
      .
      .
      Conclusion: thus including or excluding this ratio will never even matter for our ans
      .
      .
      Proof: Also proof is purely mathematically but i’ll explain with an example ( making this example generic )
      .
      .
      Step1- as all ratios are sorted thus number these ratios as r1, r2, r3, r4. Now as they are sorted just assign them random but sorted values. r1 = 1, r2 = 2, r3 = 3, r4 = 4 (you can take any ratio values but sorted)
      Step 2 - now we imagine we had to take 2 employees, and also imagine you are at 4th employee i.e. index = 3 (4th employee)
      Step 3 - (Assumption to prove the fact) - Now we are saying the ith employee has higher quality & we already have lesser k qualities in the heap. Okay, lets say ith employee has quality 5, while you already had in the heap quality of 2 & quality of 1 (imagine for index 1 & 2 respectively)
      Step 4 - Now as ratios are sorted in ascending order and index3 employee had a ratio of 4 (r4 = 4) and quality as 5 (just considered higher than 2 & 1), so wage which i'll be giving this 3rd index employee will be 5 * 4 = 20$
      Step 5 - While if we already had a quality of 2 (taking max quality out of 2 & 1), and we also know its ration will for sure be less than 3rd employee (assumed ratio was 2), thus his wage will be 2*2 = 4 (same will follow for second employee too)
      Thus, it proves, that as ratios are sorted in ascending order, this will make sure we take minRatio & multiply by minQualitySum to get minPaidSalary to these k employees.
      I hope this solves the doubt.

    • @parthgupta971
      @parthgupta971 3 місяці тому +1

      I am constantly learning new things from your videos thats why I am able to think in depth. Thank you for these awesome videos bro and awesome explanation too :)

  • @cenacr007
    @cenacr007 3 місяці тому

    Well Explained

  • @ShivamYadav-vg5fv
    @ShivamYadav-vg5fv 3 місяці тому

    How do you manage to get these questions done during a live contest?

  • @avinashkumar8147
    @avinashkumar8147 3 місяці тому

    Best explanation

  • @AmanChandna-bv4tt
    @AmanChandna-bv4tt 3 місяці тому +1

    meri selection ho gyi h?

  • @kratimishra2283
    @kratimishra2283 3 місяці тому

    Please share code solution as well

  • @tanyaraj3129
    @tanyaraj3129 3 місяці тому

    just for your reach commenting good guy

  • @kartikeyasingh5172
    @kartikeyasingh5172 3 місяці тому

    i am not getting one thing , In the code how am i making sure that i do not pick someone such that the ratio*quality of that person is lesser than his minimun wage ??

    • @ARYANMITTAL
      @ARYANMITTAL  3 місяці тому

      Already explained in video bro, when i told, club both the conditions thus we arrived on our final condition

  • @nikeshmali8506
    @nikeshmali8506 3 місяці тому

    if anyone don't want to share then just click on share button and copy the link, it will trick the algo.

  • @daveloperkrishana7559
    @daveloperkrishana7559 3 місяці тому +1

    quality* (not quantity)😭

  • @AnujGupta-xi5ep
    @AnujGupta-xi5ep 3 місяці тому +1

    Space should be :
    O(n + k)

  • @siddheshpatil5995
    @siddheshpatil5995 3 місяці тому

    bro where are we checking that the paid amount is more than minimum wage amount?

    • @nooob_coder
      @nooob_coder 3 місяці тому

      same doubt
      plz reply if got the answer

    • @jevinmakwana6811
      @jevinmakwana6811 3 місяці тому +2

      first we have taken the ratio(wage/q => wage per quality) stored in vectorworkers (dbl:ratio, int:quality). then sorted workers means for all i

    • @nooob_coder
      @nooob_coder 3 місяці тому

      @@jevinmakwana6811 got it in dry run thanks

    • @petermj2804
      @petermj2804 3 місяці тому +1

      Since we are sorting by w/q ratio,if we use w/q ratio for a quality values corresponding to higher w/q ratio,we will see that the wage for the person with high w/q ratio will go below his minimum wage value,and so this condition has to be true for every person whose w/q ratio we are looking to check.

    • @chaitanyaamajala2582
      @chaitanyaamajala2582 3 місяці тому

      @@jevinmakwana6811 Yes

  • @brp3522
    @brp3522 3 місяці тому +2

    10:30 life of a IOS user

  • @jevinmakwana6811
    @jevinmakwana6811 3 місяці тому

    class Solution {
    public:
    double mincostToHireWorkers(vector& quality, vector& wage, int k) {
    vector workers;
    int n = quality.size();
    for(int i=0; i k){
    q_sum -= q.top();
    q.pop();
    }
    if(q.size() == k)
    ans = min(ans, worker.first*q_sum);
    }
    return ans;
    }
    };

  • @aloksachan8572
    @aloksachan8572 3 місяці тому +1

    maqsad 😅

  • @praveensingh5729
    @praveensingh5729 3 місяці тому +5

    bHAI just keep the things simple, don't talk too much.
    what is becha..... this.. that....
    don't try to include extra unnecessary things.

  • @YashAgarwal-tf2rl
    @YashAgarwal-tf2rl 3 місяці тому +3

    bhai ise ganda explanation nhi dekha bol zyda rha hai samjha kuch nhi pa rha

  • @Engineering.Wallah
    @Engineering.Wallah 3 місяці тому

    Great explanation

  • @piyushsingh9320
    @piyushsingh9320 3 місяці тому

    Great Explanation !