Google Coding Interview Question and Answer - Min Cost To Hire K Workers [LeetCode 857]

Поділитися
Вставка
  • Опубліковано 29 жов 2024

КОМЕНТАРІ • 118

  • @vikasbairwa604
    @vikasbairwa604 3 роки тому +12

    The best part about this video is that she didn't go with the optimized solution from the get-go but rather gradually build up to it explaining all the optimizations.

  • @danielzhang3827
    @danielzhang3827 3 роки тому +22

    This is definitely the best explanation i've seen online! Thanks for drawing out and running through an example where each worker has a chance to be the captain. That definitely cleared up all of my confusion. Keep it up!

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

      That’s awesome! I’m glad it helped :)

  • @iamrey192
    @iamrey192 5 місяців тому +1

    Thanks. You really explained great. I was struggling to find a solution like yours. Once again thanks

  • @andrewcenteno3462
    @andrewcenteno3462 5 місяців тому

    The code is simple, but this problem is really hard to wrap your head around. She did an incredible job explaining the ratio concept at the start

  • @sambasivareddyasam5712
    @sambasivareddyasam5712 5 місяців тому +1

    the best thing about the solution is the math questions ...thank you for the solution

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

    Spent over a day trying to understand the optimized solution.
    You made it really simple.
    Thank you so much!

  • @akashsrivastava8324
    @akashsrivastava8324 3 роки тому +7

    This is the best explanation I have seen yet.

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

    This is really one of hardest questions I saw. I could neever even understand the solution without your guidance. Thank you.

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

      Yes, it’s definitely a tough one. I’m glad the video helped:)

  • @shatulbansal4756
    @shatulbansal4756 5 місяців тому

    A very tough question & the way you explained it by starting from the brute force approach & then gradually moving to optimized approach was just awesome, kudos Shiran !! Keep it up :)

  • @DavidSharma-ds
    @DavidSharma-ds 5 місяців тому

    Amazing visuals and solid explanation of the intuition. You actually explained why does the ratio of wage/quality matter and how to optimise choosing captian.

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

    Your explanation is far better than many content on youtube. Please post such odd leetcode problems in such a fantastic way.

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

      Thank you :) I’m working on a new one now. Hopefully will be up next week.

  • @khalidkhan8292
    @khalidkhan8292 5 місяців тому

    The best explanation so far. It's not about the code. As if you are solving or trying hard problems you already know how to write that. What matter here is what is the problem , how to solve the problem and why this approach is best and let me tell you, you nailed the explanation part. You earned a subscriber miss.

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

    Loved the video and the explanations, you rock!
    I also really liked the fact that contrary to what common sense dictates, the algorithm wouldn't let us choose the best and cheapest worker, since he's skewing everybody's paychecks (שובר את השוק). Keep it up 👸

  • @AniketSingh-mt6zr
    @AniketSingh-mt6zr Рік тому

    Very Nice!! I love the way u start from naive approach to the best solution and optimised the solution step wise .

  • @pushkarraj4640
    @pushkarraj4640 Рік тому +1

    Most Unique and Nicest Ever explanation seen on UA-cam for a leetcode hard Problem. Thanks a lot. Code Quality Oh My God !!👌
    U are killing it there 🔥🔥🔥🔥

  • @raviprakash4-yearb.tech.ch375
    @raviprakash4-yearb.tech.ch375 2 роки тому +1

    by your explanation i am able to do 2 question this one and other "maximum performance of a team"(leetcode) .
    great explanation . thanks a lot

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

    wow Just outstanding explaination ! after spending some years people can learn to code but very few get this skill of explaining the code.

  • @priyanshgupta6830
    @priyanshgupta6830 3 роки тому +4

    Brilliant explanation, You definitely deserves more views. Top quality content

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

      Thanks Priyansh! I’m glad you enjoyed it :)

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

    Thanks for showing the solution that won't work and then improvising. Was easier to connect the dots!

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

      You’re welcome :) I’m glad it helped!

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

    Really liked your way of explaining. Thank you so much for such an explanation. Using a variable named "Captain" was very cool.

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

      Thank you! The leetcode solution also had the “captain” worker so can’t take credit for that name :) glad you enjoyed the video!

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

    Thank you so much! This channel is by far the best I've found on the topic.

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

    Please keep uploading, your videos are among the best on youtube

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

    Explanation is really crisp and detailed. Keep up the good work.

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

    Best explanantion so far.

  • @skyisbluexd
    @skyisbluexd 5 місяців тому

    I knew this would be the best explanation. thanks

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

    Thanks for the great explanation ! Each time you applied some optimisation I felt like "dayum" . Your naming of variables was also top-notch.

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

      😆 Thank you! I’m glad you enjoyed it 😁

  • @tejaspatel2212
    @tejaspatel2212 Рік тому +1

    Why you stopped making videos?? This was such a great explanation, pls solve more leetcode questions 😅

  • @lopamudrarath7176
    @lopamudrarath7176 3 роки тому +4

    Why don't you make a playlist, it would be very helpful. You explained it very nicely. This is best.

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

      Didn’t think of that. I’ll do it, thanks :)

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

    Concise and clear. Thank you Shiran Afergan.

  • @RahulGupta-hn5cl
    @RahulGupta-hn5cl 3 роки тому +2

    Your Explanation is very nice and good approach .. plz make these type of video continuously

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

      Thanks Rahul! I got one coming up in a few days :)

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

    Great explanation. I really enjoyed the "paper" explanation and process onto code.
    Keep it up

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

    This explanation makes it so simple! Thanks!

  • @addictedgamerclashofclansa1251
    @addictedgamerclashofclansa1251 5 місяців тому

    You are great explainer! Loved it! Looking forward to more questions

  • @vishnuvardhan-md1ux
    @vishnuvardhan-md1ux 3 роки тому +1

    Very well explained in 15 minutes.

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

    Thanks a lot for this amazing video @Shiran Afergan!!!!

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

      You’re welcome :) I’m glad you enjoyed it!

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

    Explanation is top notch...keep going.

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

    YOU'RE THE BESSSTTTT!
    Thank you for making it easy to understand.

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

    You look too good to post solutions to leetcode problems * NVM*

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

    excellent explanation!! [best ever]

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

    Brilliant explanation🙂Thanks for sharing

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

    Thank you so much for such a clear explanation.

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

    And wow great solution video !

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

    Please make a video on Strobogrammatic Number II .. by the way this video was extremely helpful(literally best explanation that can be) video thankyou very much :)..

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

      Thank you! That’s great to hear :) i like your suggestion, Haven’t done a video on this type of recursion yet. Adding to my list.

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

    Really good explanation! Thanks.

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

    Awesome explanation!

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

    Your explanation is good...

  • @adityatiwari2412
    @adityatiwari2412 5 місяців тому

    Thank you so much for this video!

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

    Liked your explanation 👍👍

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

    Fantastic!

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

    Very good Explanation

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

    Brilliant Explaination. Thank you

  • @Ari-pq4db
    @Ari-pq4db 5 місяців тому

    Well Explained , thank you

  • @deveshsharma7487
    @deveshsharma7487 8 місяців тому

    Hey Shiran, could you please explain that why is discrepancy in the time complexity you mentioned in insertion of heap for the two code blocks. For the code block from line 16 to 19 you told it is O(k) and for 23 to 34 you told it is nlogK, Why is is O(k) although we are inserting in the heap which acutally takes O(log of size of heap). Please explain this. Thanks a lot!

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

    At 13:15, I liked the video!

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

    You are so cute and this explanation is so awesome!! Love this!!
    However, should captainRatio in line 20 be workers[k-1].first? Since I thought k-1 should be captain in the first place. Though its no big deal since the answer is correct lol.

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

      It is! Great catch, it’s a bug that i fixed later in the video (fix is at time: 14:57)

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

    Thank you so much. This is super clear!

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

    Great explanation! keep going

  • @vineetkumar2899
    @vineetkumar2899 5 місяців тому

    that's awesome

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

    HEy Also ! How are we always sure that there will be a person whi can be the captain of the team (i.e form a team of size k) because even in this example we saw how 2 peoplw were not able to make a team unless we would somehow pay more than his expected wage and make him the captain

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

    Thanks!

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

    9:15 Why not just use a minHeap(negative of what you are pushing into the maxheap) and pop the top K?

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

      That will also work. but the complexity is different. What you suggest has O(n + klogn) time and O(n) space. what i did in the video is O(k + (n-k)logk) time and O(k) space.

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

      @@ShiranAfergan I usually use std::multiset for the heap. Under the hood it's implemented using RB tree so the time complexity would be similar. The great thing about it is that you can pop the last element using minHeap.erase(minHeap.end()--) It also allow you to find an arbitrary element using minHeap.find().
      Great explanation btw. I was so lucky to find your channel :)

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

    subscribed

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

    please tell me how quickly you were able to come up to this solution ? Amazing!!!!

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

      I don’t remember but I can tell you I highly doubt I’d be able to come up with this during a 45 minutes interview 😆

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

      @@ShiranAfergan thank you, my confidence has lowered after seeing this problem and the way you solved it😅 I was no way I can do this.

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

    Better than the stupid leetcode editorial

  • @c.4469
    @c.4469 2 роки тому

    I think this is the hardest one.

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

    Why only compare the quality[k] to quality[n-k] with maxHeap.top() instead of every element in maxHeap ?

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

      so Why do we choose the highest quality person to remove? Why not choosing other workers?

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

      Because we want the heap to contain the k smallest qualities. Let’s say we have k=3, heap={1,2,4} and current quality is 3.
      If we were to replace 3 with anything other than the largest quality (4), the heap will not contain the 3 smallest qualities (1,2,3)

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

      @@ShiranAfergan Thank you so much for replying, but what if we have k=3,heap={2,4,5}, and current quality is 3, and there are more elments coming after the current elment, if that's the case, shouldnt we pop out 4 and 5 the elements and push 3 then push 4 again to make {2,3,4} in the heap ?
      if we only choose the highest quality person to replace, does it mean we have k-1 elements already optimal in the heap ?
      I am so confused.

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

      Yes, after each iteration, the heap will contain the k smallest elements we have seen up until that point and not necessarily in the whole array. So only when we finish looping over the entire array, the heap will contain the k smallest elements in the array.
      If you’re still confused, try contradicting this assumption. Start like this - assume that the heap does not contain the k smallest elements. Meaning there is at least one element x, which is one of the k smallest elements but is not in the heap. So either x was never inserted to the heap or it was inserted and later replaced. Can one of these options really happen?

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

    Man !! Effortlessly cute

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

    I hate math problems so much...

  • @JamesBond-mq7pd
    @JamesBond-mq7pd 5 місяців тому

    who is from May 11th 2024?

  • @VishalKumar-xr4nm
    @VishalKumar-xr4nm 2 роки тому

    Here is my code:
    class Solution {
    public double mincostToHireWorkers(int[] quality, int[] expWage, int k) {
    double mincost = Double.MAX_VALUE;
    int n = quality.length;
    double[][] workers = new double[n][2];
    for (int i=0;i Double.compare(a[0], b[0]));

    PriorityQueue pq = new PriorityQueue(Comparator.reverseOrder());
    double qsum = 0;

    for(int captain = 0;captain k){
    qsum -= pq.poll();
    }
    if(pq.size() < k) continue;
    mincost = Math.min(qsum * ratio ,mincost);
    }

    return mincost;
    }
    }