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

  Рет қаралды 7,019

Aryan Mittal

Aryan Mittal

2 ай бұрын

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/aryan-0077
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

Пікірлер: 67
@ARYANMITTAL
@ARYANMITTAL 2 ай бұрын
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 2 ай бұрын
Bhaiya you are amazing teacher...
@shivchauhan795
@shivchauhan795 2 ай бұрын
Bechara was personal🤣. Thanku pura question ache se samajh aa gya.
@mrityunjoybarman9098
@mrityunjoybarman9098 2 ай бұрын
Ohhh god when will I be able to solve these problems all by myself. But great explanation
@shikharchaturvedi5093
@shikharchaturvedi5093 2 ай бұрын
One of the best explanation of a hard problem i ever saw. Thanks bro. Keep up the Good work!
@lavanyam3224
@lavanyam3224 2 ай бұрын
The best explanation! was fun to watch how u called the lowest wage person bechara😂
@lavanyam3224
@lavanyam3224 2 ай бұрын
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 2 ай бұрын
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 2 ай бұрын
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
@gursimransingh3707
@gursimransingh3707 2 ай бұрын
bhayia slides v share krdea kro... helpfull raheta hai... notes bnane mai
@ushapandey1088
@ushapandey1088 2 ай бұрын
Honeslty i derive the formula that u derive , but uske baad how to use it i learnt from u👍..this is my learning
@kandariarjun
@kandariarjun 2 ай бұрын
You explained it very well. Thanks a lot.
@vishwakarma-gaurav
@vishwakarma-gaurav 2 ай бұрын
Amazing 🔥🔥
@IK-xk7ex
@IK-xk7ex 2 ай бұрын
Thank you for explanation.
@prathameshautade2679
@prathameshautade2679 2 ай бұрын
Great Explanation, Thanks
@chinmoydebnath353
@chinmoydebnath353 2 ай бұрын
good luck, coming up with this during interview😮‍💨
@dhineshd94
@dhineshd94 2 ай бұрын
best explanation for this problem
@rajrajesh1669
@rajrajesh1669 2 ай бұрын
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?
@manoharmanu9240
@manoharmanu9240 2 ай бұрын
Great explanation 🙌
@akshayawasthi599
@akshayawasthi599 2 ай бұрын
great work brother.. hats off..
@piyushsingh9320
@piyushsingh9320 2 ай бұрын
Great Explanation !
@prathamjain8937
@prathamjain8937 2 ай бұрын
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
@ShivamYadav-vg5fv
@ShivamYadav-vg5fv 2 ай бұрын
How do you manage to get these questions done during a live contest?
@iamnoob7593
@iamnoob7593 21 күн бұрын
Brilliant Aryan.
@navinbarnwal4782
@navinbarnwal4782 2 ай бұрын
Nice explanation ❤
@abiraljain7313
@abiraljain7313 2 ай бұрын
Great explanation 🔥🔥🤩
@yuvikasingh9635
@yuvikasingh9635 2 ай бұрын
Excellent explanation
@HIMANSHUYADAV-dt2kb
@HIMANSHUYADAV-dt2kb 2 ай бұрын
Excellent explanation😃😃
@avinashkumar8147
@avinashkumar8147 2 ай бұрын
Best explanation
@parthgupta971
@parthgupta971 2 ай бұрын
@ARYANMITTAL what happen if the i th element got removed from priority queue?
@ARYANMITTAL
@ARYANMITTAL 2 ай бұрын
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 2 ай бұрын
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 2 ай бұрын
Well Explained
@MichaBalcerak
@MichaBalcerak 2 ай бұрын
What should I do when I don't have such an intuition for questions like that ?
@floatingpoint7629
@floatingpoint7629 2 ай бұрын
your explanation was really good
@floatingpoint7629
@floatingpoint7629 2 ай бұрын
it would have been great if you could have dry run a complete example at the end
@iamnoob7593
@iamnoob7593 21 күн бұрын
Request you to kindly also do LC 759 , 871.
@Anushka20037
@Anushka20037 Ай бұрын
i don't get him after 18:00
@kartikeyasingh5172
@kartikeyasingh5172 2 ай бұрын
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 2 ай бұрын
Already explained in video bro, when i told, club both the conditions thus we arrived on our final condition
@ompatel2212
@ompatel2212 2 ай бұрын
you should share the notes
@mukulkhanna5071
@mukulkhanna5071 2 ай бұрын
This dialogue holds for this awsome video "aryan bhai ke age koi bol skta hai ky....."
@ARYANMITTAL
@ARYANMITTAL 2 ай бұрын
📢🫡📈
@prashantsahu5117
@prashantsahu5117 2 ай бұрын
hello ji just search for that video got it in just NOW
@ARYANMITTAL
@ARYANMITTAL 2 ай бұрын
Bilkul ji ❤, thank you ji 🫂
@tanyaraj3129
@tanyaraj3129 2 ай бұрын
just for your reach commenting good guy
@kratimishra2283
@kratimishra2283 2 ай бұрын
Please share code solution as well
@nikeshmali8506
@nikeshmali8506 2 ай бұрын
if anyone don't want to share then just click on share button and copy the link, it will trick the algo.
@AmanChandna-bv4tt
@AmanChandna-bv4tt 2 ай бұрын
meri selection ho gyi h?
@asmitshukla4649
@asmitshukla4649 2 ай бұрын
bhaiya jhoot ni bolunga, ekdam sir k uppar se gaya ye question................LEETCODE HARD NI BANTE HAIN MUJHSE😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭
@AnujGupta-xi5ep
@AnujGupta-xi5ep 2 ай бұрын
Space should be : O(n + k)
@jevinmakwana6811
@jevinmakwana6811 2 ай бұрын
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; } };
@siddheshpatil5995
@siddheshpatil5995 2 ай бұрын
bro where are we checking that the paid amount is more than minimum wage amount?
@nooob_coder
@nooob_coder 2 ай бұрын
same doubt plz reply if got the answer
@jevinmakwana6811
@jevinmakwana6811 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 2 ай бұрын
@@jevinmakwana6811 got it in dry run thanks
@petermj2804
@petermj2804 2 ай бұрын
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 2 ай бұрын
@@jevinmakwana6811 Yes
@brp3522
@brp3522 2 ай бұрын
10:30 life of a IOS user
@aloksachan8572
@aloksachan8572 2 ай бұрын
maqsad 😅
@daveloperkrishana7559
@daveloperkrishana7559 2 ай бұрын
quality* (not quantity)😭
@ARYANMITTAL
@ARYANMITTAL 2 ай бұрын
🥲🙈
@sangdilbiswal30
@sangdilbiswal30 2 ай бұрын
I was getting confused if quantity was another thing
@YashAgarwal-tf2rl
@YashAgarwal-tf2rl 2 ай бұрын
bhai ise ganda explanation nhi dekha bol zyda rha hai samjha kuch nhi pa rha
@praveensingh5729
@praveensingh5729 2 ай бұрын
bHAI just keep the things simple, don't talk too much. what is becha..... this.. that.... don't try to include extra unnecessary things.
@Engineering.Wallah
@Engineering.Wallah 2 ай бұрын
Great explanation
@ShristiSethiya-ch2he
@ShristiSethiya-ch2he 2 ай бұрын
Great Explanation!!
Minimum Cost to Hire K Workers - Leetcode 857 - Python
19:01
NeetCodeIO
Рет қаралды 12 М.
Clown takes blame for missing candy 🍬🤣 #shorts
00:49
Yoeslan
Рет қаралды 38 МЛН
아이스크림으로 체감되는 요즘 물가
00:16
진영민yeongmin
Рет қаралды 60 МЛН
Best KFC Homemade For My Son #cooking #shorts
00:58
BANKII
Рет қаралды 56 МЛН
Smart Sigma Kid #funny #sigma #comedy
00:26
CRAZY GREAPA
Рет қаралды 6 МЛН
2.6.3 Heap - Heap Sort - Heapify - Priority Queues
51:08
Abdul Bari
Рет қаралды 2 МЛН
My Brain after 569 Leetcode Problems
7:50
NeetCode
Рет қаралды 2,5 МЛН
The LeetCode Fallacy
6:08
NeetCode
Рет қаралды 449 М.
Is the C programming language still worth learning?
9:27
Jacob Sorber
Рет қаралды 91 М.
Clown takes blame for missing candy 🍬🤣 #shorts
00:49
Yoeslan
Рет қаралды 38 МЛН