Climbing the Leaderboard

Sort by

recency

|

99 Discussions

|

  • + 0 comments

    java

        public static List<Integer> climbingLeaderboard(List<Integer> ranked, List<Integer> player) {
            // Write your code here
            HashSet<Integer> set = new HashSet<>(ranked);
            ranked = new ArrayList<>(set); // only uniq value 
            Collections.sort(ranked , Collections.reverseOrder());
            ArrayList<Integer> result = new ArrayList<>();
            for(int p : player)
            {
                int index = Collections.binarySearch(ranked , p , Collections.reverseOrder());
                if(index < 0)
                {
                    index = Math.abs(index);
                    ranked.add(index -1 , p );
                    result.add(index);
                }else{
                    result.add(index + 1) ;
                }
                
            }
            return result ;   
        }
    
  • + 1 comment

    I do not understand the solution for the example: The existing scores are [100,90,90,80], The new players are [70,80,105]. The combined score board should be [105,100,90,80,70], so that the return should be [5,4,1] instead of [4,3,1].

  • + 0 comments

    Problem with Javascript rendered this unsolvable

  • + 0 comments

    Simple work around to achieve O(n) time and space complexity taking advantage of the fact that the array is already sorted. Beats the other solutions since we do not have to sort the array again or run binary search n times. Same space complexity as we have to make a new array for the solution anyway.

    def climbingLeaderboard(ranked, player):
        cleanRanked = []
        for score in ranked:
            if len(cleanRanked) > 0 and cleanRanked[-1] == score:
                continue
            cleanRanked.append(score)
            
        res = []
        r = len(cleanRanked) - 1
        for score in player:
            while r >= 0 and score >= cleanRanked[r]:
                r -= 1
            res.append(r+2)
        
        return res
    
  • + 0 comments

    Binary search would do and turns out it was pretty straight forward. Here is my snippet in Javascript.

    function binarySearch(arr, val) {
        let start = 0;
        let end = arr.length - 1;
    
        while (start <= end) {
            const mid = Math.floor((start + end) / 2);
    
            if (arr[mid] === val) {
                return mid + 1; // Found the exact value
            } else if (val > arr[mid]) {
                end = mid - 1; // Search in the left half
            } else {
                start = mid + 1; // Search in the right half
            }
        }
    
        // Determine the rank based on the insertion position
        return start + 1;
    }
    
    function climbingLeaderboard(ranked, player) {
        // Write your code here
        const uniqRanked = [...new Set(ranked)];
        return player.map((score) => {
            return binarySearch(uniqRanked, score);
        });
    }