Luck Balance | Greedy Algorithms | Hackerrank Solution | Python | Interview Preparation Kit

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome to mud codes and in this video we're going to solve the question luck balance it's a part of the interview preparation kit on hack rank and we're going to solve it using python if you aren't already subscribed then do subscribe to my channel for coding and other tech related content with that let's get started lena is preparing for an important coding competition that is preceded by a number of sequential preliminary contests initially her luck balance is zero she believes in saving luck and wants to check her theory each contest is described by two integers so this question is written in a pretty complex form so i'll just explain it in very simple terms lena believes that she can save her luck and that luck can be used to win other contests in the future so initially her luck balance is zero and there are two arrays l i and t i so li is the amount of luck associated with each contest so the size of both the arrays will be same each index is a contest and l of i is the amount of luck associated with that contest and t of i is the importance of that contest so if it's equal to one then it's important if it's zero it's unimportant and we have to determine that if lena loses no more than k important contest what is the maximum amount of luck she can have after competing in all the preliminary contests so this value can be negative or positive as she can be out of luck as well so basically l is the amount of luck that she can gain by losing a contest and over here they have said that k determines the maximum number of contests she can lose which are important so it's only for important contests where t is equal to one not for t equal to zero so let's look at this example over here just a second it's not clearly visible so i'll change this over here you can see that k is equal to 2 so she can lose two important contests and the luck for each of the contest is 5 1 and 4 so over here this two is also important itself so over here they've given one it i think it's a printing mistake so as you can see over here she can definitely lose the unimportant contest so she'll lose that and get four and since she can lose a maximum of two contests she will lose both of them as well and the total luck she can have at the end is 10. so if k was one over here then she would have won this contest over here which is one luck itself so she would have won this contest over here which has a luck factor of one and it won't be that her luck would be five plus four nine it will subtract this luck factor from it and it will become 9 minus 1 which is 8. so if you lose a contest the luck is added but if you win it is subtracted so this is why it can also be negative so the function has two parameters one is k and one is a 2d array of integers contests where the size is n and each index of contest contains another array of size 2 which has l and t so the first index is the luck and the second index is the importance and we have to return the maximum luck balance achievable so now let's get started so over here the first thing that we will do is we will initialize a variable called luck to zero as they've said that initially luck will always be zero and after that the contest that we have so i'll just show you how it looks like contests we'll run this so this is how the contests look like so this is the luck factor five this is the importance 2 is the luck factor 1 is the importance and so on so we will first sort contests so let's sort contest now so contests dot sort and we'll sort it in the reverse order so we are sorting it in the reverse order because first entry will have the maximum luck and the last entry will have the least luck so lena has to lose the first ones and if the last ones are unimportant or like fall within the range of k then she loses the end ones so now we will start accumulating her luck first we'll go through the contests so for contest in contests and now this contest contains this value so this contest is this entire entry which has the first index as luck and second index has the t value which is the importance so now we will first add the luck of all the contests which have the importance zero because those she can definitely lose and accumulate luck so we'll do that if contest at the first index which means the second index but at position one because the first position is zero so at this index if the value is zero then we add the luck factor to the luck variable so now we have accumulated all the luck of the unimportant competitions and now we will use the variable k and accumulate the luck from as many competitions as possible up until k is exhausted so else if k is greater than 0 so as long as k has a positive value we add the luck to the final variable because the first entries are going to have the highest luck factor so we want to accumulate all of that as much as possible so we will accumulate that and we will subtract k so now for the first index say if k was 2 for the first index if the importance was 1 it will accumulate it and k will reduce for the second index the same will happen but after that if there are any important competitions then lena will participate and win in them because she can't lose more than k competitions so once this is done and k is exhausted luck is subtracted so now luck will be subtracted every time she wins a competition and she has to win because k is exhausted and she doesn't have a choice so now she will win these competitions and luck will be subtracted but since these are the smallest luck values as they come towards the end of the array contests so it won't affect as much as it would if we subtracted the larger ones so once all of this is done and we come out of the loop we simply return luck and this should have the maximum luck that lena can achieve so let's run the code and check yep so it's running fine now let's see if all the test cases are running or not let's submit it yep so it's a success and lana can have a luck so if you like this video then do hit the like button and you can check out the playlist over here on top for the interview preparation kit series i'm building the entire series and it will be done soon so that playlist will help you in your preparation you can save it to your library and see you in the next video bye
Info
Channel: Mud Codes
Views: 33
Rating: undefined out of 5
Keywords: array, python, interview, hackerrank, hackerrank tutorial, python tutorial, data structures, coding, programming, tutorial, google, facebook, amazon, coding camp, hackathon, interview tips, interview questions and answers, hackerrank solution, solution, algorithm, cracking the coding interview, ctci, interview preparation kit, python beginner to advance, luck balance hackerrank solution python, luck balance hackerrank solution, luck balance, greedy algorithms, luck balance solution
Id: Yid_YvwiVw4
Channel Id: undefined
Length: 8min 5sec (485 seconds)
Published: Wed Sep 15 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.