Sort by

recency

|

2331 Discussions

|

  • + 0 comments

    Python, O(n+m)

    def climbingLeaderboard(ranked, player):
        ranks = []
        ranked = sorted(list(set(ranked)))
        currRank = len(ranked)+1
        for i in player:
            while ranked and i >= ranked[0]:
                del ranked [0]
                currRank -= 1
            ranks.append(currRank)
        return ranks
    

    First the list is converted to a set and then back to a list to remove all dupes. Then loops through each element of players, deprecating the ranking and deleting every value of ranked smaller than the player value. The while loop is only ever entered len(ranked) times.

  • + 0 comments

    C# Code

    I guess O(n²) doesn't work on C#. Time limit exceed error. Perfect logic though I think.

    public static List<int> climbingLeaderboard(List<int> ranked, List<int> player)
        {
            List<int> ranking = new List<int>();
            int num_of_players = player.Count();
            ranked = ranked.Distinct<int>().OrderByDescending(s => s).ToList();
            
            foreach(int p in player)
            {
                int temp_rank = 1;
                for(int i = 0; i < ranked.Count; i++)
                {
                    if(p<ranked[i])
                    {
                        temp_rank++;
                    }
                    else
                    {
                        break;
                    }
                }
                ranking.Add(temp_rank);
            }
            return ranking;
        }
    
  • + 0 comments

    Python solution

    def bin_search(rank, sc):
        start, end = 0, len(rank) -1
    
        while start <= end:
            middle = (start + end)// 2
            if rank[middle] == sc:
                return middle
            elif rank[middle] < sc:
                end = middle -1
            else:
                start = middle + 1
    
        return start
    
    
    def climbingLeaderboard(ranked, player):
    
        ranked = list(set(ranked))
        ranked = sorted(ranked, reverse= True)
        res = []
        index = 0
        for i in player:
            index = bin_search(ranked, i)
            res.append(index +  1)
    
    
        return res
    
  • + 1 comment

    def climbingLeaderboard(ranked, player): ranked = sorted(set(ranked),reverse=True) index = len(ranked) - 1 output = [] for i in player: while index >= 0 and i >= ranked[index]: index -= 1 output.append(index+2)
    return output

  • + 0 comments
    def climbingLeaderboard(ranked, player):
        ranked = sorted(set(ranked), reverse=True)
        negated_list = [-x for x in ranked]
    
        
        
        result = []
        for p in player:
            # Binary search to find the correct rank
            try:
                result.append(ranked.index(p)+1)
            except:
                result.append(bisect.bisect_left(negated_list,-p)+1)
        return result