• + 22 comments

    Python 2

    n = int(raw_input().strip())
    scores = map(int,raw_input().strip().split(' '))
    m = int(raw_input().strip())
    alice = map(int,raw_input().strip().split(' '))
    
    leaderboard = sorted(set(scores), reverse = True)
    l = len(leaderboard)
    
    for a in alice:
        while (l > 0) and (a >= leaderboard[l-1]):
            l -= 1
        print l+1
    
    • + 0 comments

      I had a much longer program at first, and then came across your solution. Very elegant! Thanks for sharing

    • + 8 comments

      Here is my Python 3 solution using the bisect library.

      from bisect import bisect_right
      
      n = int(input())
      scores = sorted(set(map(int,input().split())))
      m = int(input())
      alice = map(int,input().split())
      
      # your code goes here
      for i in alice:
          print(len(scores)-bisect_right(scores,i)+1)
      
      • + 0 comments

        great one

      • + 0 comments

        awsome one

      • + 1 comment

        perfect

        • + 0 comments

          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])

      • + 0 comments

        perfect

      • + 0 comments

        I came up with this solution in Python3, hope can help anyone too...

         def climbingLeaderboard(ranked, player):
              rank = list(set(ranked))
              rank.sort()
              player.sort()
              resp = list()
              x = len(rank)+1
              for item in player:
                   resp.append(x - bisect.bisect(rank, item))
              return resp
        
      • + 0 comments

        Great !!!!!! thanks for introducing me to bisect

      • + 0 comments

        I too used same logic but it didn't worked

      • + 0 comments

        here is my long code (lol):

        def climbingLeaderboard(ranked, player):
            ranked = sorted(set(ranked), reverse=True)
            a,n,l = [],0,len(ranked)
            
            for i in player[::-1]:
                for j in range(n, l):
                    if i >= ranked[n]:
                        break
                    n += 1
                a.append(n+1)
        
            return a[::-1]
        
    • + 1 comment

      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.

      • + 0 comments

        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

    • + 0 comments

      Very cool, thank you for sharing.

    • + 0 comments

      very elegant indeed!

    • + 0 comments

      it has timeout error

    • + 2 comments

      Mine is so similar to your solution: But I have to append a zero at the end. You bet me by that.

      #!/bin/python3
      
      import sys
      
      if __name__ == "__main__":
          n = int(input().strip())
          scores = sorted(list(set(list(map(int, input().strip().split(' '))))), reverse=True)
          m = int(input().strip())
          alice = list(map(int, input().strip().split(' ')))
          scores.append(0)
          l = len(scores)
          # Write Your Code Here
          for a in alice:
              while (l > 0) and (a >= scores[l-1]):
                  l -= 1
              print(l+1)
      
      • + 0 comments

        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.

      • + 2 comments

        def climbingLeaderboard(scores, alice): # Complete this function n=len(scores) levels=len(alice)

        sl=list(set(scores))
        sl.sort(reverse=True)
        
        for i in alice:
            flag= False
            sl.append(i)
            c=sl.count(i)
            if(c>1):
                flag= True
            ds=set(sl)
            sl=list(ds)
            sl.sort(reverse=True)
            Rank=sl.index(i)
            print(Rank+1)
            if(flag == True):
                pass
            else:
                sl.remove(i)
        

        Question : How to get rid of timeout issue in the above code

        • + 0 comments

          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.

        • + 0 comments

          i wrote almost same kind of solution but it didnt work for large inputs how will it be solved

    • + 1 comment

      I think the key that I was missing is to make the array distinct. But WHY does making the array distinct solve the problem?

      • + 0 comments

        you can easily get your index in list as rank you need not do more may be

    • + 2 comments

      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!

      def climbingLeaderboard(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 scores
          for a in alice:
              
              # 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)
              
          return results
      
      • + 0 comments

        Pretty clear. Thanks

      • + 0 comments

        This helped me a lot. BIG THANKS

    • + 0 comments

      your code is giving eof error

    • + 0 comments

      I think this is a very good program, but it has the problem with arrays of type [25, 5, 50, 10]

    • + 0 comments

      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)

    • + 0 comments
      1. 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)
    • + 0 comments

      I though this would cause TLE becuase O(N) complexity in each search?

    • + 0 comments

      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.

    • + 0 comments

      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

    • + 0 comments

      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.

    • + 0 comments

      awesome

    • + 0 comments

      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.

    • + 0 comments

      veryyyy nicely done bro