Тёмный

Climbing The Leaderboard HackerRank Solution 

JAVAAID - Coding Interview Preparation
Подписаться 37 тыс.
Просмотров 34 тыс.
50% 1

Опубликовано:

 

22 авг 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 122   
@JavaAidTutorials
@JavaAidTutorials 5 лет назад
JAVAAID community: RU-vid recently changed the way they monetize my content. My channel now needs 1,000 subscribers so it would be awesome if you could show your support by both watching my videos and subscribing to my channel if you haven’t already done so. Monetizing my videos allows me to invest back into the channel with new equipment so a small gesture from you goes a huge way for me. Many thanks for your support!
@nirmalcherian6691
@nirmalcherian6691 4 года назад
Sir. What if every score of alice is higher than the greatest score in score board?
@NITINAGAM
@NITINAGAM 3 года назад
Finally earned 20 points after submitting this solution. That's the power of Binary search. Thank you !!
@anugrahmasih6347
@anugrahmasih6347 2 года назад
I was finally able to pass all the test cases in C++. Here is my implementation of the binary search method and the method we had to fill on hackerrank int binary_search(vector &ranked, int key, vector &rank){ if(key>ranked[0]) return 1; else if(key
@Strange2309
@Strange2309 2 года назад
That was helpful ...thanks
@delicious_lunch3823
@delicious_lunch3823 4 года назад
Great explanation, thanks! For posterity (using Java SE 8), I had a timeout failure on test case 7 even when doing binary search. I had put the binary search in it's own method for readability, and I passed it the score to be tested, the ranking array, and the scores array as parameters (so I only made the ranking array once, and then just passed it as a parameter). When I pulled the binary search into the solution method, it ran within the timing parameter. I guess I was riding right on the edge, and taking out the extra method calls (which I don't believe added a whole lot of complexity at run time) got me in the window.
@JavaAidTutorials
@JavaAidTutorials 4 года назад
Thanks for your feedback but i did not get it, whether you are able to pass all Test case s or not?
@delicious_lunch3823
@delicious_lunch3823 4 года назад
@@JavaAidTutorials Oh, sorry, yes I was able to get through all test cases, and actually learned quite a bit! Thanks! Yeah I tried to do it without the line by line implementation details (so, I paused after you explained how it worked in broad terms, without seeing the exact implementation), and got further then I had before, but still missed one test case. After seeing your implementation and better mirroring that, I passed the last remaining test case.
@JavaAidTutorials
@JavaAidTutorials 4 года назад
I am glad to know that finally all test cases got passed.
@pranavchandrode7127
@pranavchandrode7127 2 года назад
I use C++, while using vectors test case 6 and 7 were timed out so try giving a chance to array and worked fine, I don't know the reason behind it but my code worked. Thank man!!
@crvinva
@crvinva 3 года назад
The first condition should check for >= instead of > otherwise testcase 5 fails: scores=[1], alice=[1,1]. if (aliceScore >= scores[0]) res[i] = 1; But this is a great explanation. Thank you.
@astroflexx82
@astroflexx82 Год назад
Thank you for the solution sir!! My solution was very close to this, the only thing I couldn't figure out how to modify the Binary Search algorithm.
@AlokSingh-gg9wc
@AlokSingh-gg9wc 3 года назад
sir, test case 6 is failed(TLE) in c++. I followed exactly the same approach as you have taught.
@tanmaysinghal8370
@tanmaysinghal8370 4 года назад
I don't even know how to it with the basic approach lol
@annelee4743
@annelee4743 2 года назад
Thanks for your detailed explanation. It really helps! I clicked 'Subscribe' Button haha
@flexobender
@flexobender 4 года назад
Thank you very much! Your idea to make an array RANK realy helped! And binary search of cause.
@JavaAidTutorials
@JavaAidTutorials 4 года назад
most welcome.. 🙂
@RoronoaZoro_ViceCapt_Sunny
@RoronoaZoro_ViceCapt_Sunny 4 года назад
implemented the same solution in c and got two test case failures. here is the code for reference: int binarysearch(int n,vector arr) { int lo=0,hi=arr.size()-1,mid; while(lon) lo=mid+1; else if(arr[mid]
@RoronoaZoro_ViceCapt_Sunny
@RoronoaZoro_ViceCapt_Sunny 4 года назад
your solution does work for java 8 though. if possible please implement in c.
@JavaAidTutorials
@JavaAidTutorials 4 года назад
I am not well versed with C. but can give you a hint you can reduce search space every time as arrays content is ordered.
@ashishdas5986
@ashishdas5986 4 года назад
Great Video.Helpled me a lot to progress further
@JavaAidTutorials
@JavaAidTutorials 4 года назад
Great to hear!
@dkiranreddy
@dkiranreddy 4 года назад
hello sir ,nice explanation , in this explanation if 120 number got first rank then automatically 100 will get 2nd and 50 get 3rank is it rt? ..but in your example the ranks are not changing?..please explain.
@gameonline6769
@gameonline6769 3 года назад
Can we just use a treemap to store the ranks initially and then instead of us doing the binary search, we can just take floorKey and ceilingKey and calculate accordingly.
@laodearissaputra5317
@laodearissaputra5317 4 года назад
great tutorial. but can you add subtitle for the video? Thank you
@JavaAidTutorials
@JavaAidTutorials 4 года назад
Thanks for your feedback. I am working on the subtitles, will add it soon.
@MuffyA
@MuffyA 2 года назад
If done in python, the entire search operation can be handled by the bisect method in the bisect library
@codingfreek5737
@codingfreek5737 5 лет назад
Great video...!!!
@JavaAidTutorials
@JavaAidTutorials 5 лет назад
Thank you.
@akshatojha7791
@akshatojha7791 4 года назад
Thanks a lot sir. I was thinking of exactly same solution but was messing up things in binary search.
@JavaAidTutorials
@JavaAidTutorials 4 года назад
Welcome 😊
@mohammadhammad99
@mohammadhammad99 4 года назад
Great explanation
@JavaAidTutorials
@JavaAidTutorials 4 года назад
Glad it was helpful!
@mohammadhammad99
@mohammadhammad99 4 года назад
@@JavaAidTutorials can i get c++ code
@Tx3Stones
@Tx3Stones 3 года назад
Thanks for great video. I do have a question. After 120 is inserted, aren't we supposed to update the rest ranks since 120 is now #1, 100 #2, and so on?
@JavaAidTutorials
@JavaAidTutorials 3 года назад
this is how the problem wants :)
@rudrasama297
@rudrasama297 3 года назад
its useless to update because there is only one player with different score so only his rank will update. By Updating there score you will get wrong rank.
@srinjoyghosh7273
@srinjoyghosh7273 4 года назад
The Kanhaiya we need...
@kushalkumar9569
@kushalkumar9569 3 года назад
superb...!
@saxena3718
@saxena3718 3 года назад
i don't understand why in the second else if you have used ">=" and why not just ">. Please reply
@ec_7006
@ec_7006 3 года назад
How can we do this using ArrayList instead? They have changed the parameters.
@darshmamtora2863
@darshmamtora2863 4 года назад
didn't understood the 2nd else if statement in binary search... if key is smaller than mid than how it can be greater than or equal to mid+1 element
@RoronoaZoro_ViceCapt_Sunny
@RoronoaZoro_ViceCapt_Sunny 4 года назад
because scores array is in descending order
@louisadibe3189
@louisadibe3189 4 года назад
Cool video
@JavaAidTutorials
@JavaAidTutorials 4 года назад
thank you.:)
@yaswanthkosuru
@yaswanthkosuru Год назад
it takes o(n) space
@rahulshukla6748
@rahulshukla6748 5 лет назад
Good explanation
@JavaAidTutorials
@JavaAidTutorials 5 лет назад
Thanks @Rahul
@neeleshlakhera7524
@neeleshlakhera7524 4 года назад
but this program give the wrong ans in case score{100,70,50,45,23} or Alice { 55,100,110,120} for all 100 or 110 and a120 gives the same result 1 1 1 ?
@JavaAidTutorials
@JavaAidTutorials 4 года назад
can you please cross verify your code with my algorithm once again, probably you would have missed something or may be doing something extra which is not required.
@chaoluncai1607
@chaoluncai1607 2 года назад
hello, I used the exact implementation except changing the scores array to list (Since that's the method parameter type) however 8 tests failed: Here's the code I've done, basically just the array to list conversion : private static int binarySearch(List ranked, int key) { int lo = 0; int hi = ranked.size() - 1; while (lo key && key >= ranked.get(mid+1)) { return mid + 1; } else if (ranked.get(mid) < key) { hi = mid - 1; } else if (ranked.get(mid) > key) { lo = mid + 1; } } return -1; } public static List climbingLeaderboard(List ranked, List player) { // Write your code here int[] alice = player.stream().mapToInt(i->i).toArray(); int n = ranked.size(); int m = alice.length; int res[] = new int[m]; int[] rank = new int[n]; rank[0] = 1; for (int i = 1; i < n; i++) { if (ranked.get(i) == ranked.get(i-1)) { rank[i] = rank[i - 1]; } else { rank[i] = rank[i - 1] + 1; } } for (int i = 0; i < m; i++) { int aliceScore = alice[i]; if (aliceScore > ranked.get(0) ) { res[i] = 1; } else if (aliceScore < ranked.get(n-1) ) { res[i] = rank[n - 1] + 1; } else { int index = binarySearch(ranked, aliceScore); res[i] = rank[index]; } } return Arrays.stream(res).boxed().collect(Collectors.toList()); } I'm in desperate trying to debug, the 2 sample cases passed with no problem tho, I'd be really appreciate if you could try my code on HackerRank and see if you could find out the bug, Thank you!
@chaoluncai1607
@chaoluncai1607 2 года назад
ohhh I forgot I'm comparing the reference to these integer wrapper class not the actual intvalue tho... the test passed soon as I changed to Integer.equals() method which has already been overridden in default , sorry for the confusion!
@shreyask8107
@shreyask8107 2 года назад
@@chaoluncai1607 i didn’t get exactly what you changed ... can pls explain or mention what and where u changed ? Thank you
@grovestreet9165
@grovestreet9165 2 года назад
i tried this method and it passes all the test cases vector climbingLeaderboard(vector ranked, vector player) { vector ranks; // here storing the player rank vector uranked; //here getting only unique values int i,j; for( i=0;i=0 && j
@lutallica83
@lutallica83 4 года назад
Thanks for posting your solution; however, I still don't understand what the subtask actually means. Does it mean that 60% of all cases are between 0 and 200 players (n) or Alice games (m)?
@mukuldhariwal1906
@mukuldhariwal1906 5 лет назад
which software is used to draw the content and explain ?
@JavaAidTutorials
@JavaAidTutorials 5 лет назад
I use Microsoft OneNote as my drawing board.
@keshavniketh7060
@keshavniketh7060 3 года назад
scores=[100,90,90,80] alice=[85,95,97] code is not for this test case i guess rank should be 4,3,2 it is giving output as 3,2,2
@JavaAidTutorials
@JavaAidTutorials 3 года назад
If you read out the question carefully , you will find that for same score rank should be same. so for 90,90 it will be same. 100,90,90,85...here 85 comes at 3rd place because both scored 90 and will get 2nd rank.
@devisakthikrishnakumar7713
@devisakthikrishnakumar7713 5 лет назад
Cool 👍🏽
@JavaAidTutorials
@JavaAidTutorials 5 лет назад
Thanks..
@curesnow6493
@curesnow6493 3 года назад
Can you please do FlatLand SpaceStation? I’m very confused on that one...
@JavaAidTutorials
@JavaAidTutorials 3 года назад
Will try covering that.
@rajathnayak5624
@rajathnayak5624 4 года назад
how did it occur to u that u should perform binary search? nice explanation tho kudos :)
@JavaAidTutorials
@JavaAidTutorials 4 года назад
If array is sorted, binary search is the first option which comes to find to improve complexity..😊
@Rockyzach88
@Rockyzach88 2 года назад
It's the fastest way to insert a number in the correct order. Iterating over the whole list takes too long when you get really long lists (arrays). Interestingly, I remember when I was learning to troubleshoot circuits, you did something just like this but they called it "half splitting".
@yashsalvi5043
@yashsalvi5043 2 года назад
Does anyone know what should be the optimization for C++, in order to get the 6th test case passed ?
@srivathsav4587
@srivathsav4587 Год назад
Did you find it out?
@maheshsadashiv5606
@maheshsadashiv5606 5 лет назад
your rank array will be wrong if the scores array has more than 2 of the same scores eg {100,100,100,90,90,90} in this case your rank array will be {1,1,1,2,2,2} but it should be {1,1,2,3,3,4}
@JavaAidTutorials
@JavaAidTutorials 5 лет назад
Hi Mahesh, I just ran my program against below input and here is the answer. Input (stdin) 6 100 100 100 90 90 90 6 1 1 1 2 2 2 Your Output (stdout) 3 3 3 3 3 3 which is totally fine. I hope now your doubt would be clear about climbing the leaderboard problem.
@ecarrillo
@ecarrillo 4 года назад
thank you so much
@JavaAidTutorials
@JavaAidTutorials 4 года назад
welcome 🙂
@josejayant3127
@josejayant3127 3 года назад
I implemented your code using c++ but there was one test case that showed "timed out" it was test case 6
@JavaAidTutorials
@JavaAidTutorials 3 года назад
try to paste your code in comments may be people would like see and will share their feedback.
@lakshyapatel3842
@lakshyapatel3842 3 года назад
@Jose Jayant same man same
@lakshyapatel3842
@lakshyapatel3842 3 года назад
@@JavaAidTutorials my code is: #include using namespace std; int bin_search(vector vec1, int num1){ int high = vec1.size() - 1; int low = 0; while(low num1 && vec1[mid+1] < num1){ return mid + 1; } else if(vec1[mid] < num1 && num1 < vec1[mid - 1]){ return mid; } else if (vec1[mid] > num1){ low = mid + 1; } else if ( vec1[mid] < num1){ high = mid - 1; } } return -1; } vector climbingLeaderboard(vector ranked, vector player) { int ranked_size = ranked.size(); int player_size = player.size(); vector ranks; ranks.push_back(1); for(int i = 1; i < ranked_size; i++){ if(ranked[i - 1] == ranked[i]){ ranks.push_back(ranks[i-1]); } else{ ranks.push_back(ranks[i - 1] + 1); } } vector player_rank; for(int j = 0; j < player_size; j++){ if(player[j] > ranked[0]){ player_rank.push_back(1); } else if(player[j] < ranked[ranked_size - 1]){ player_rank.push_back(ranks[ranked_size - 1] + 1); } else{ int index1 = bin_search(ranked,player[j]); player_rank.push_back(ranks[index1]); } } return player_rank; } int main() { int leaderboard_size; cin >> leaderboard_size; vector leaderboard; int e1; for(int i = 0; i < leaderboard_size; i++){ cin >> e1; leaderboard.push_back(e1); } int game_size; cin >> game_size; vector game_score; for(int j = 0; j < game_size; j++){ cin >> e1; game_score.push_back(e1); } vector player_rank1 = climbingLeaderboard(leaderboard,game_score); for(int x = 0; x < game_size; x++){ cout
@amrmoneer5881
@amrmoneer5881 4 года назад
thanks so much for this. you made this exercise really simple. thanks again, sir.
@JavaAidTutorials
@JavaAidTutorials 4 года назад
most welcome..😊 if you find our channel helpful, please support us by sharing with your friends.
@smartcoder1112
@smartcoder1112 5 лет назад
I also scored the 20 marks..😀
@JavaAidTutorials
@JavaAidTutorials 5 лет назад
It sounds good.
@roasterhubb
@roasterhubb Год назад
TLE hai sir ji apke code me
@uvso5429
@uvso5429 5 лет назад
can anyone explain will this logic will work if in alice array their are more then one number greater then score[0] value will all number greater then score[0] will get first rank because no where we are updating scores array
@JavaAidTutorials
@JavaAidTutorials 5 лет назад
Sorry did not get your question.is this solution is not working out for you?
@uvso5429
@uvso5429 5 лет назад
@@JavaAidTutorials suppose in alice array two numbers are greater then scores zero index value as you are checking boundary condition first will both the number will get 1st rank as both are greater then scores Zero Index value
@JavaAidTutorials
@JavaAidTutorials 5 лет назад
@@uvso5429So here Alice array stores the score achieved by Alice for every game. in Alice array, every index shows the kind of gaming level. index 0 is like in level 1 what he scored, index 1 means level2 what he scored and you have to find the proper rank after every level. so you can not consider more than one entry from Alice array per game.
@syedzulfaquaralam
@syedzulfaquaralam 3 года назад
public static List climbingLeaderboard(List ranked, List player) { int m=ranked.size(); int n=player.size(); int rank[]=new int[m]; List res=new ArrayList(); rank[0]=1; for(int i=1;ialiceScore && aliceScore>=ranked.get(mid+1)) { return mid+1; } else if(aliceScore>ranked.get(mid)) { high=mid-1; } else if(aliceScore
@keshavniketh7060
@keshavniketh7060 3 года назад
this is my code Listresult=new ArrayList(player.size()); ListRank=new ArrayList(); int hi=ranked.size()-1; Rank.add(0,1); for(int i=1;i= ranked.get(hi)) hi -= 1; if (hi == -1) result.add(i,1); else result.add(i,Rank.get(hi)+1); } return result; }
@gehnaanand9996
@gehnaanand9996 5 лет назад
It gives a timeout error in cpp for testcase 6 and 7
@JavaAidTutorials
@JavaAidTutorials 5 лет назад
just wanted to confirm, you are using the same algorithm which I have showed or something else?
@gehnaanand9996
@gehnaanand9996 5 лет назад
@@JavaAidTutorials yes same algorithm
@JavaAidTutorials
@JavaAidTutorials 5 лет назад
hi@@gehnaanand9996, In C, C++ given execution time is less as compared to JAVA and Python, so that may be the reason. It's passing all TCs in JAVA but not in C++ even for the same algo. while solving this problem, I have missed one important info in problem statement just because of my solution works, I did not use that info at all in my solution. So what you can do you can use that info and make your code optimize to reduce the execution time. hint--In the problem statement, it's given that "Alice's scores, are in ascending order." in that case you do not need to apply binary search. you can reduce the search space by using this statement. so whenever you find rank for the first time, that will be the minimum rank which Alice can get because his score is going to get better and better. so no need to check the element after the previous rank he got.
@kunalsharma5124
@kunalsharma5124 3 года назад
Please Provide the Climbing the Leaderboard Solution using List method class Result { /* * Complete the 'climbingLeaderboard' function below. * * The function is expected to return an INTEGER_ARRAY. * The function accepts following parameters: * 1. INTEGER_ARRAY ranked * 2. INTEGER_ARRAY player */ public static List climbingLeaderboard(List ranked, List player) { // Write your code here ranking =[] leaderboard = sorted(set(scores)t, reverse = True) l = len(leaderboard) for a in alice: while (l > 0) and (a >= leaderboard[l-1]): l -= 1 rankings.append(l + 1) return rankings } public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); int rankedCount = Integer.parseInt(bufferedReader.readLine().trim()); String[] rankedTemp = bufferedReader.readLine().replaceAll("\\s+$", "").split(" "); List ranked = new ArrayList(); for (int i = 0; i < rankedCount; i++) { int rankedItem = Integer.parseInt(rankedTemp[i]); ranked.add(rankedItem); } int playerCount = Integer.parseInt(bufferedReader.readLine().trim()); String[] playerTemp = bufferedReader.readLine().replaceAll("\\s+$", "").split(" "); List player = new ArrayList(); for (int i = 0; i < playerCount; i++) { int playerItem = Integer.parseInt(playerTemp[i]); player.add(playerItem); } List result = Result.climbingLeaderboard(ranked, player); for (int i = 0; i < result.size(); i++) { bufferedWriter.write(String.valueOf(result.get(i))); if (i != result.size() - 1) { bufferedWriter.write(" "); } } bufferedWriter.newLine(); bufferedReader.close(); bufferedWriter.close(); } } Please correct the Class result code and provide the Solution. I am getting compile time Error.
@anjaliagrawal9587
@anjaliagrawal9587 3 года назад
Thank you for such a precise explanation ❤️🙏🏻
@AnupamHaldkar
@AnupamHaldkar 4 года назад
Could You help I am getting time out on 3 cases: static int[] climbingLeaderboard(int[] scores, int[] slice) { int rank[] = new int[scores.length]; rank[0]=1; for(int i=1;i
@JavaAidTutorials
@JavaAidTutorials 4 года назад
There is one information which was missing in problem statement but provided in example that scores are in descending order.
@AnupamHaldkar
@AnupamHaldkar 4 года назад
@@JavaAidTutorials I have used same approach
@balajibaheti8577
@balajibaheti8577 4 года назад
optimised but still fail to pass case 6,7,9 code: int f,l; int bs(vectors,int f,int l,int x) { int as=0; while(l>=f) { int m=(f+l)/2; if(x>=s[m]) { if(x
@JavaAidTutorials
@JavaAidTutorials 4 года назад
follow the approach explained in this video, you will be able to pass all TCs.
@balajibaheti8577
@balajibaheti8577 4 года назад
@@JavaAidTutorials got error!if you are passing vector to function then pass it as pass by reference!
@pauliusgasparavicius2004
@pauliusgasparavicius2004 5 лет назад
Thank you for your help. Tip : pls don"t explain like we are 5 years old because it's getting boring to watch :)
@JavaAidTutorials
@JavaAidTutorials 5 лет назад
Hi Paulius Gasparavicius , Its good to hear feedback from you so that I can do some improvement in my upcoming content. But can you elaborate a little bit more? like I have three sections where you feel I explained like the 5-year-old kid..:) in all sections or a particular section.
@shat104
@shat104 5 лет назад
@@JavaAidTutorials your explanation is great.
@JavaAidTutorials
@JavaAidTutorials 5 лет назад
@@shat104 thanks, @Fat to Flat
@koushalgarg2398
@koushalgarg2398 5 лет назад
its not working in cpp . It gives time error
@JavaAidTutorials
@JavaAidTutorials 5 лет назад
Hi Koushal, May i know your algorithm?
@Aysh_Sach_16-2
@Aysh_Sach_16-2 5 лет назад
New content would be appreciated
@JavaAidTutorials
@JavaAidTutorials 5 лет назад
Hi, For new content, check out the channel, you will find the new content on weekend. Yesterday we have upload dynamic programming tutorials.
@harshmiglani5385
@harshmiglani5385 4 года назад
one test case still not working. you wasted my time.
@josejayant3127
@josejayant3127 3 года назад
was it test case 6?
Далее
Bigger Is Greater HackerRank Solution
11:50
Просмотров 15 тыс.
New Year Chaos HackerRank Solution
23:56
Просмотров 52 тыс.
He Didn’t Know My Mom is a Chess Grandmaster…
5:55
Sherlock and array
22:40
Просмотров 1,1 тыс.
How To Solve HackerRank Drawing Book Problem
11:00
Просмотров 17 тыс.