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

  Рет қаралды 15,095

Shiran Afergan

Shiran Afergan

3 жыл бұрын

In this video I explain the solution of a Google programming interview question “Minimum Cost To Hire K Workers”.
You can find it here - leetcode.com/problems/minimum...
Link to the solution code -
github.com/shiran-afergan/You...

Пікірлер: 118
@vikasbairwa604
@vikasbairwa604 2 жыл бұрын
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.
@ShiranAfergan
@ShiranAfergan 2 жыл бұрын
Glad you enjoyed it 😊
@andrewcenteno3462
@andrewcenteno3462 2 ай бұрын
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
@danielzhang3827
@danielzhang3827 3 жыл бұрын
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 жыл бұрын
That’s awesome! I’m glad it helped :)
@sambasivareddyasam5712
@sambasivareddyasam5712 2 ай бұрын
the best thing about the solution is the math questions ...thank you for the solution
@shatulbansal4756
@shatulbansal4756 2 ай бұрын
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 :)
@raghavsood768
@raghavsood768 2 жыл бұрын
Spent over a day trying to understand the optimized solution. You made it really simple. Thank you so much!
@iamrey192
@iamrey192 2 ай бұрын
Thanks. You really explained great. I was struggling to find a solution like yours. Once again thanks
@iulianabinzar8297
@iulianabinzar8297 Жыл бұрын
Thank you so much! This channel is by far the best I've found on the topic.
@akashsrivastava8324
@akashsrivastava8324 3 жыл бұрын
This is the best explanation I have seen yet.
@ShiranAfergan
@ShiranAfergan 3 жыл бұрын
Thank you! That’s great to hear:)
@dincerbeken5761
@dincerbeken5761 2 жыл бұрын
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:)
@pranjalabhishek7566
@pranjalabhishek7566 3 жыл бұрын
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.
@pareshpandit802
@pareshpandit802 Жыл бұрын
This explanation makes it so simple! Thanks!
@skyisbluexd
@skyisbluexd 2 ай бұрын
I knew this would be the best explanation. thanks
@priyanshgupta6830
@priyanshgupta6830 3 жыл бұрын
Brilliant explanation, You definitely deserves more views. Top quality content
@ShiranAfergan
@ShiranAfergan 3 жыл бұрын
Thanks Priyansh! I’m glad you enjoyed it :)
@KidCrippler
@KidCrippler Жыл бұрын
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 👸
@ShiranAfergan
@ShiranAfergan Жыл бұрын
Thanks! Glad you liked it 🙂
@DavidSharma-ds
@DavidSharma-ds 2 ай бұрын
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.
@harryscorner7135
@harryscorner7135 2 жыл бұрын
Thanks a lot for this amazing video @Shiran Afergan!!!!
@ShiranAfergan
@ShiranAfergan 2 жыл бұрын
You’re welcome :) I’m glad you enjoyed it!
@addictedgamerclashofclansa1251
@addictedgamerclashofclansa1251 2 ай бұрын
You are great explainer! Loved it! Looking forward to more questions
@siddharthasriramvinjam167
@siddharthasriramvinjam167 3 жыл бұрын
Concise and clear. Thank you Shiran Afergan.
@ShiranAfergan
@ShiranAfergan 3 жыл бұрын
Thanks 😊 glad you liked it
@raviprakash4-yearb.tech.ch375
@raviprakash4-yearb.tech.ch375 2 жыл бұрын
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
@ShiranAfergan
@ShiranAfergan 2 жыл бұрын
That’s awesome! 💪🏽😎
@khalidkhan8292
@khalidkhan8292 2 ай бұрын
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.
@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 😁
@chaunguyen8202
@chaunguyen8202 2 жыл бұрын
YOU'RE THE BESSSTTTT! Thank you for making it easy to understand.
@ShiranAfergan
@ShiranAfergan 2 жыл бұрын
Thanks 🙏🏽 😊
@ptr2587
@ptr2587 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 жыл бұрын
Thank you! The leetcode solution also had the “captain” worker so can’t take credit for that name :) glad you enjoyed the video!
@jishulayek8252
@jishulayek8252 3 жыл бұрын
Explanation is really crisp and detailed. Keep up the good work.
@ShiranAfergan
@ShiranAfergan 3 жыл бұрын
Thank you :)
@vxz90044
@vxz90044 3 жыл бұрын
Great explanation. I really enjoyed the "paper" explanation and process onto code. Keep it up
@ShiranAfergan
@ShiranAfergan 3 жыл бұрын
That’s awesome! Thanks:)
@adityatiwari2412
@adityatiwari2412 2 ай бұрын
Thank you so much for this video!
@pushkarraj4640
@pushkarraj4640 Жыл бұрын
Most Unique and Nicest Ever explanation seen on KZbin for a leetcode hard Problem. Thanks a lot. Code Quality Oh My God !!👌 U are killing it there 🔥🔥🔥🔥
@ShiranAfergan
@ShiranAfergan Жыл бұрын
Thank you!! 🙏🏽🙏🏽
@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 .
@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.
@ShiranAfergan
@ShiranAfergan 2 жыл бұрын
Thank you! 😊
@harishsn4866
@harishsn4866 Жыл бұрын
Thank you so much for such a clear explanation.
@habilismayilov838
@habilismayilov838 Жыл бұрын
excellent explanation!! [best ever]
@tongyuyang2073
@tongyuyang2073 3 жыл бұрын
Thank you so much. This is super clear!
@ShiranAfergan
@ShiranAfergan 3 жыл бұрын
I’m glad it helped:)
@tejaspatel2212
@tejaspatel2212 Жыл бұрын
Why you stopped making videos?? This was such a great explanation, pls solve more leetcode questions 😅
@ankoor
@ankoor 3 жыл бұрын
Awesome explanation!
@sbrahma0
@sbrahma0 Жыл бұрын
Best explanantion so far.
@RadGopalakrishnan
@RadGopalakrishnan 3 жыл бұрын
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!
@siddharth4069
@siddharth4069 2 жыл бұрын
Please keep uploading, your videos are among the best on youtube
@ShiranAfergan
@ShiranAfergan 2 жыл бұрын
Thank you! 😊
@lopamudrarath7176
@lopamudrarath7176 3 жыл бұрын
Why don't you make a playlist, it would be very helpful. You explained it very nicely. This is best.
@ShiranAfergan
@ShiranAfergan 3 жыл бұрын
Didn’t think of that. I’ll do it, thanks :)
@poojapalod
@poojapalod Жыл бұрын
Brilliant explanation🙂Thanks for sharing
@shrimpo6416
@shrimpo6416 2 жыл бұрын
Fantastic!
@ShiranAfergan
@ShiranAfergan 2 жыл бұрын
🙂
@satviksharma4897
@satviksharma4897 3 жыл бұрын
Really good explanation! Thanks.
@ShiranAfergan
@ShiranAfergan 3 жыл бұрын
Thanks 🙏🏽
@Ari-pq4db
@Ari-pq4db 2 ай бұрын
Well Explained , thank you
@pwnweb5734
@pwnweb5734 2 жыл бұрын
Brilliant Explaination. Thank you
@vishnuvardhan-md1ux
@vishnuvardhan-md1ux 3 жыл бұрын
Very well explained in 15 minutes.
@ShiranAfergan
@ShiranAfergan 3 жыл бұрын
Thanks:)
@RahulGupta-hn5cl
@RahulGupta-hn5cl 3 жыл бұрын
Your Explanation is very nice and good approach .. plz make these type of video continuously
@ShiranAfergan
@ShiranAfergan 3 жыл бұрын
Thanks Rahul! I got one coming up in a few days :)
@snehilgupta1558
@snehilgupta1558 3 жыл бұрын
Explanation is top notch...keep going.
@ShiranAfergan
@ShiranAfergan 3 жыл бұрын
Thank you :)
@rajatbansal7431
@rajatbansal7431 3 жыл бұрын
And wow great solution video !
@ShiranAfergan
@ShiranAfergan 3 жыл бұрын
Thanks!
@jionghongli7826
@jionghongli7826 3 жыл бұрын
Great explanation! keep going
@ShiranAfergan
@ShiranAfergan 3 жыл бұрын
Thank you!
@vineetkumar2899
@vineetkumar2899 2 ай бұрын
that's awesome
@pawandeepchor89
@pawandeepchor89 3 жыл бұрын
Liked your explanation 👍👍
@ShiranAfergan
@ShiranAfergan 3 жыл бұрын
Thanks for watching :)
@hapysethi1306
@hapysethi1306 2 жыл бұрын
Thanks!
@executenow4211
@executenow4211 3 жыл бұрын
Your explanation is good...
@ShiranAfergan
@ShiranAfergan 3 жыл бұрын
Thanks!
@sidharthbansal5835
@sidharthbansal5835 3 жыл бұрын
Very good Explanation
@ShiranAfergan
@ShiranAfergan 3 жыл бұрын
Thanks :)
@rajatbansal7431
@rajatbansal7431 3 жыл бұрын
You look too good to post solutions to leetcode problems * NVM*
@ShiranAfergan
@ShiranAfergan 3 жыл бұрын
:)
@VasudevaK
@VasudevaK Жыл бұрын
At 13:15, I liked the video!
@himanshuchhikara4918
@himanshuchhikara4918 3 жыл бұрын
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.
@almasabdrazak5089
@almasabdrazak5089 2 жыл бұрын
subscribed
@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
@deveshsharma7487
@deveshsharma7487 5 ай бұрын
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!
@litaibaikidmort
@litaibaikidmort 3 жыл бұрын
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)
@fluffymattress5242
@fluffymattress5242 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 2 жыл бұрын
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 :)
@c.4469
@c.4469 2 жыл бұрын
I think this is the hardest one.
@Opportunity631
@Opportunity631 2 жыл бұрын
please tell me how quickly you were able to come up to this solution ? Amazing!!!!
@ShiranAfergan
@ShiranAfergan 2 жыл бұрын
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 жыл бұрын
@@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.
@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 жыл бұрын
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?
@prashantjha439
@prashantjha439 3 жыл бұрын
Better than the stupid leetcode editorial
@ShiranAfergan
@ShiranAfergan 3 жыл бұрын
Thanks :)
@sriramkrishnamurthy4473
@sriramkrishnamurthy4473 2 жыл бұрын
Man !! Effortlessly cute
@symbol767
@symbol767 2 жыл бұрын
I hate math problems so much...
@JamesBond-mq7pd
@JamesBond-mq7pd 2 ай бұрын
who is from May 11th 2024?
@VishalKumar-xr4nm
@VishalKumar-xr4nm Жыл бұрын
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; } }
Does size matter? BEACH EDITION
00:32
Mini Katana
Рет қаралды 20 МЛН
Nastya and SeanDoesMagic
00:16
Nastya
Рет қаралды 12 МЛН
100❤️
00:19
MY💝No War🤝
Рет қаралды 23 МЛН
Data Structures for Coding Interviews [In 10 Minutes]
10:40
Shiran Afergan
Рет қаралды 41 М.
Top 10 Data Analyst Interview Questions (with answers)
15:58
The CAP Theorem | System Design Concepts for Beginners
5:35
Shiran Afergan
Рет қаралды 9 М.
Full Thought Process | LeetCode 287. Find the Duplicate Number | Part 1
11:06
How to understand your Google offer letter
24:32
Kadima Careers
Рет қаралды 4,9 М.
Does size matter? BEACH EDITION
00:32
Mini Katana
Рет қаралды 20 МЛН