We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Here is my Python 3 solution using the bisect library.
frombisectimportbisect_rightn=int(input())scores=sorted(set(map(int,input().split())))m=int(input())alice=map(int,input().split())# your code goes hereforiinalice:print(len(scores)-bisect_right(scores,i)+1)
My code using dict() but time complexity is not good, Any help
n = int(input())
scores = sorted(set(map(int,input().split())))
m = int(input())
alice = map(int,input().split())
for first in alice:
d={}
rank=1
scores.append(first)
scores.sort(reverse=True)
d[scores[0]]=1
for i in range(1,len(scores)):
if scores[i]==scores[i-1]:
d[int(scores[i])]=rank
else:
rank=rank+1
d[int(scores[i])]=rank
print(d[first])
can you explain me how the while condition always satisfy
i think somewhere it does not satisfy but then also it goes in while loop. But your solution is best.
I think does not work when we have something like [25, 5, 30, 10], but given the fact that the problem defines alice scroes on increasing order that should not be a problem, however I think it may fail when we have something like max_score_leader_board = Z and [5, 10, Z, Z+A, Z+B] acording to my calculus it should give for Z, Z+A and Z+B the rank 1
Just a small detail, there is no need to convert the set into list if you are going to use the sorted method, you can simple use sorted(set(iterable)).
sorted(): Return a new sorted list from the items in iterable.
the method "count" will scan the entire data structure, which means O(n), since you are doing it for each element, that means we have O(n**2), consider using hashing somehow.
Here is an annotated version of albiewalbie's solution, but in Python3 and applied within the definition of the function.
I hope this helps others with the logic!
defclimbingLeaderboard(scores,alice):# List to contain and return Alice's ranks.results=[]# Unique values from scores, since duplicate scores will have same rank (their index value).leaderboard=sorted(set(scores),reverse=True)# Use this var to track index within leaderboard later.l=len(leaderboard)# Loop through each of Alice's scoresforainalice:# If Alice's score is >= the score at the index of the end of leaderboard...# Subtract 1 from that index value (which is also the rank) to check the next score up.# If the score is less than the next score up, the index (rank) will be added to results.while(l>0)and(a>=leaderboard[l-1]):l-=1# We add 1 to the appended value to account for 0-indexing.results.append(l+1)returnresults
I have used two for loops in my solution and it failed in four test cases due to time complexity.you used one while and for loops but it turned out it is error clear how is this happening,what is the principle in time complexity.
scores=list(map(int,input().split()))
alice=list(map(int,input().split()))
climberank=[]
for i in alice:
temp=set(sorted(scores))
temp.add(i)
for index,value in enumerate(sorted(temp,reverse=True),1):
if value==i:
climberank.append(index)
temp.discard(i)
print(*climberank)
scores=list(map(int,input().split()))
alice=list(map(int,input().split()))
climberank=[]
for i in alice:
temp=set(sorted(scores))
temp.add(i)
for index,value in enumerate(sorted(temp,reverse=True),1):
if value==i:
climberank.append(index)
temp.discard(i)
print(*climberank)
Correct me if I am worng, I think this solution works only when the first element in the player is less than last element of ranked array. Remaining elements of player array sholud be greater than the least element of ranked array.
For example:
if ranked= [100 90 90 80 75 60] and player = [50 55 65 77 90 102] it doesnt satisfy.
Thumbs up for this solution, the day we reach to use while loops and it's subsequent increments or decrements, it will become a cakewalk to solve such problems.
This code works only because test cases are flawed. This wouldn't work if player (alice) list has 2 or more values that are greater than maximum value of ranked (leaderboard) list.
Climbing the Leaderboard
You are viewing a single comment's thread. Return to all comments →
Python 2
I had a much longer program at first, and then came across your solution. Very elegant! Thanks for sharing
Here is my Python 3 solution using the bisect library.
great one
awsome one
perfect
My code using dict() but time complexity is not good, Any help n = int(input()) scores = sorted(set(map(int,input().split()))) m = int(input()) alice = map(int,input().split()) for first in alice: d={} rank=1 scores.append(first) scores.sort(reverse=True) d[scores[0]]=1 for i in range(1,len(scores)): if scores[i]==scores[i-1]: d[int(scores[i])]=rank else: rank=rank+1 d[int(scores[i])]=rank print(d[first])
perfect
I came up with this solution in Python3, hope can help anyone too...
Great !!!!!! thanks for introducing me to bisect
I too used same logic but it didn't worked
here is my long code (lol):
can you explain me how the while condition always satisfy i think somewhere it does not satisfy but then also it goes in while loop. But your solution is best.
I think does not work when we have something like [25, 5, 30, 10], but given the fact that the problem defines alice scroes on increasing order that should not be a problem, however I think it may fail when we have something like max_score_leader_board = Z and [5, 10, Z, Z+A, Z+B] acording to my calculus it should give for Z, Z+A and Z+B the rank 1
Very cool, thank you for sharing.
very elegant indeed!
it has timeout error
Mine is so similar to your solution: But I have to append a zero at the end. You bet me by that.
Just a small detail, there is no need to convert the set into list if you are going to use the sorted method, you can simple use sorted(set(iterable)).
sorted(): Return a new sorted list from the items in iterable.
def climbingLeaderboard(scores, alice): # Complete this function n=len(scores) levels=len(alice)
Question : How to get rid of timeout issue in the above code
the method "count" will scan the entire data structure, which means O(n), since you are doing it for each element, that means we have O(n**2), consider using hashing somehow.
i wrote almost same kind of solution but it didnt work for large inputs how will it be solved
I think the key that I was missing is to make the array distinct. But WHY does making the array distinct solve the problem?
you can easily get your index in list as rank you need not do more may be
Here is an annotated version of albiewalbie's solution, but in Python3 and applied within the definition of the function.
I hope this helps others with the logic!
Pretty clear. Thanks
This helped me a lot. BIG THANKS
your code is giving eof error
I think this is a very good program, but it has the problem with arrays of type [25, 5, 50, 10]
I have used two for loops in my solution and it failed in four test cases due to time complexity.you used one while and for loops but it turned out it is error clear how is this happening,what is the principle in time complexity.
scores=list(map(int,input().split())) alice=list(map(int,input().split())) climberank=[] for i in alice: temp=set(sorted(scores)) temp.add(i) for index,value in enumerate(sorted(temp,reverse=True),1): if value==i: climberank.append(index) temp.discard(i) print(*climberank)
I though this would cause TLE becuase O(N) complexity in each search?
Correct me if I am worng, I think this solution works only when the first element in the player is less than last element of ranked array. Remaining elements of player array sholud be greater than the least element of ranked array. For example: if ranked= [100 90 90 80 75 60] and player = [50 55 65 77 90 102] it doesnt satisfy.
wow, neat and powerful. Solves the time issue. so, it basically cuts down the list which is being searched when the a in alice is seen
Thumbs up for this solution, the day we reach to use while loops and it's subsequent increments or decrements, it will become a cakewalk to solve such problems.
awesome
This code works only because test cases are flawed. This wouldn't work if player (alice) list has 2 or more values that are greater than maximum value of ranked (leaderboard) list.
veryyyy nicely done bro