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!
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
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 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.
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!!
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.
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]
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.
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.
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?
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.
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 ?
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.
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!
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!
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
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)?
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.
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".
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}
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.
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 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
@@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.
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
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; }
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.
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.
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
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
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.