Sort by

recency

|

2342 Discussions

|

  • + 1 comment

    my solution:

    https://github.com/Tusenka/hackerrank/blob/main/climbing_leaderboard.py

  • + 0 comments

    this is my go solution, some extra lines maybe buy very good performance.

        cleanRanked := func(ranked []int32) []int32 {
            newRank := make([]int32, 0)
            for i := 0; i < len(ranked)-1; i++ {
                if ranked[i] != ranked[i+1] {
                    newRank = append(newRank, ranked[i])
                }
            }
            newRank = append(newRank, ranked[len(ranked)-1])
            return newRank
        }(ranked)
    
        j := 0
        var position int32 = 1
        positions := make([]int32, len(player))
    
        for i := len(player) - 1; i >= 0; i-- {
            for j <= len(cleanRanked)-1 && cleanRanked[j] > player[i] {
                if j == len(cleanRanked)-1 || cleanRanked[j] > cleanRanked[j+1] {
                    position++
                }
                j++
            }
            positions[i] = position
        }
    
        return positions
    }
    
  • + 0 comments

    I aimed to make the code traverse both lists only once, and this was the result. Any suggestions for improvement?

    def climbingLeaderboard(ranked, player):
        ranked = sorted(set(ranked), reverse=True)
        player = sorted(player, reverse=True)
        player_ranks = []
        
        i = 0
        
        for p in player:
            while i <= len(ranked):
                if i == len(ranked):
                    player_ranks.append(i + 1)
                    break
                elif p >= ranked[i]:
                    player_ranks.append(i + 1)
                    break
                i += 1
    
        return sorted(player_ranks, reverse=True)
    
  • + 0 comments
    def climbingLeaderboard(ranked, player):
        org_rank = sorted(set(ranked), reverse=True) 
        ans = []
        rank_i = len(org_rank) - 1 
        for score in player:
            while rank_i >= 0 and score >= org_rank[rank_i]:
                rank_i -= 1
            ans.append(rank_i + 2)  
        return ans
    
  • + 0 comments
    • The idea is to create a set of ranked with distinct values
    • Now we start from the backward of the leadership board to find the position for the player score, that by reducing the index
    • Once we find the score position then insert index + 2 to the resulted array

    function climbingLeaderboard(ranked, player) { // Write your code here const leaderboard = Array.from(new Set(ranked)) const result = [] let index = leaderboard.length - 1 // Start from the lowest rank

    for (let score of player) {
      // Move up the leaderboard until you find the position for the player's score
      while (index >= 0 && score >= leaderboard[index]) {
        index--
      }
      // The player's rank is index + 2 (since index + 1 would be 0-based)
      result.push(index + 2)
    }
    
    return result;
    

    } `