Sort by

recency

|

2358 Discussions

|

  • + 0 comments

    This short Python solution takes advantage of the fact that the player scores are listed in ascending order. We first make a sorted list of unique leader scores, then for each player score, removing the elements which are smaller than the player score, the length of the remaining leader scores plus one is thus the rank of the player score. The process is continued with the shorten leader scores list, thus saves a lot of steps.

    def climbingLeaderboard(ranked, player):
        ranks = []
        new_ranked = list(set(ranked.copy()))
        new_ranked = sorted(new_ranked)
        for score in player:
            while new_ranked and score >= new_ranked[0]:
                new_ranked.pop(0)
            ranks.append(len(new_ranked) + 1)        
        return ranks
    
  • + 0 comments

    My code doesn't pass the execution time limits, does anyone know why?. Here it is:

    '''finds the position of the player based on their score. We compare their score with the scores of all the other players.'''
    
    def order(ranked, element):    
        storage=[]
        storage.append((1, ranked[0]))
        index=2
        for score in ranked[1:]:
            if storage[-1][1]==score:
                continue
            else:
                ele=(index, score)
                storage.append(ele)
                index+=1        
        for pair in storage:        
            if (pair[1]==element):
                return pair[0] 
    
    '''
    This will insert the player's score in its correct position in the ranked list. Then it will find the position of that score in the ranked list using the order function. This will be stored in the outList, which is what will be returned 
    '''
    def climbingLeaderboard(ranked, player):
        # Write your code here
        outList = []
        for score in player:
            flag=True
            if score > ranked[0]:
                ranked.insert(0, score)
                outList.append(order(ranked, score))
                continue
            for j in range(0, len(ranked)-1):
                if ranked[j] >= score >= ranked[j+1]:
                    ranked.insert(j+1, score)
                    outList.append(order(ranked, score))
                    flag = False
                    break
            if flag:
                ranked.append(score)
                outList.append(order(ranked, score))
        return outList
    
  • + 0 comments
    public static List<Integer> climbingLeaderboard(List<Integer> ranked, List<Integer> player) {
    
       ArrayList<Integer> list = new ArrayList<>(new TreeSet<>(ranked).descendingSet());
       List<Integer> answer = new ArrayList<Integer>();
       for(Integer x: player){
            int index = Collections.binarySearch(list, x, Collections.reverseOrder());
            if(index<0){
                index = Math.abs(index);
            }
            else{
                 index+=1;
            }
            answer.add(index);
       }
       return answer;
    }
    
  • + 0 comments

    Simple solution using STL:lowerbound

    vector<int> climbingLeaderboard(vector<int> ranked, vector<int> player) {
        // Removing duplicates
        auto end = unique(ranked.begin(), ranked.end());
        
        vector<int> out;
        // Checking lowerbound of elements of player in ranked
        for(auto j:player){
            int i = distance(ranked.begin(), lower_bound(ranked.begin(), end, j,greater<int>()));
            out.push_back(i+1);
        }
        return out;
    }
    
  • + 0 comments

    My TypeScript. Probably could still be optimized, but I took an approach that takes advantage of the player array being sorted. i.e. As you iterate, the next score will either be the same or higher, so there is no need to go back through the whole ranked set each time to find the new ranking.

    function climbingLeaderboard(ranked: number[], player: number[]): number[] {
            const board: number[]  = [...new Set(ranked)];
            const res: number[] = [];
            let n: number = board.length;
    
            player.forEach((v) => {
                    while (v > board[n-1]) {
                            if (n === 1) {
                                    res.push(1);
                                    return;
                            }
                            n--;
                    }
    
                    res.push(v === board[n-1] ? n : n + 1);
            });
    
            return res;
    }