Sort by

recency

|

2380 Discussions

|

  • + 0 comments
    def getRank(arr, target):
        start = 0
        end = len(arr) - 1
        while start <= end:
            mid = (start + end) // 2
            if target == arr[mid]:
                return mid + 1
            elif target > arr[mid] and (mid == 0 or target < arr[mid-1]):
                return mid + 1
            elif target < arr[mid]:
                start = mid + 1
            else:
                end = mid
        return len(arr) + 1
    
    def climbingLeaderboard(ranked, player):
        # Write your code here
        unique_ranked = []
        previous = None
        for i in ranked:
            if i != previous:
                previous = i
                unique_ranked.append(i)
        ranks = []    
        for i in player:
            rank = getRank(unique_ranked, i)
            ranks.append(rank)
            
        return ranks
    
  • + 0 comments

    Remove duplicates in ranked array and then sort the player array in descending order. iterate through ranked array until find the point which is less than the player's, and append the (index + 1) into the result array. Finally sort the result in descending order.

    def climbingLeaderboard(ranked, player):
        # Write your code here
        player.sort(reverse = True)
        res = []
        index = 0
        ranked = list(dict.fromkeys(ranked))
        for i in player:
            while index < len(ranked) and ranked[index] > i:
                index += 1
            if index >= len(ranked):
                index = len(ranked)
                res.append(index + 1)
                continue
            res.append(index + 1)
        res.reverse()
        return res
    
  • + 0 comments

    Remove duplicates from rank array, use binary search to find closest number to the new alice score. time complexity O(n + mlogn) :)

    int binarySearch(int* ranked, int ranked_count, int cur_rank)
    {
        int l=0;
        int r=ranked_count-1;
        int m;
        
        while (l<=r)
        {
            m = l + (r-l)/2;
        
            if (ranked[m] == cur_rank)
            {
                return m+1;
            }
            else if (ranked[m] > cur_rank)
            {
                l = m + 1;
            }
            else if (ranked[m] < cur_rank)
            {
                r = m - 1;
            }
        }
        
        return l+1;
    }
    
    int* climbingLeaderboard(int ranked_count, int* ranked, int player_count, int* player, int* result_count) {
        int* ranked_opt = (int*)malloc(sizeof(int) * ranked_count);
        int len = 0;
        for (int i=0; i<ranked_count; i++)
        {
            if (len != 0 && ranked[i] == ranked_opt[len-1]) continue;
            ranked_opt[len++] = ranked[i];
        }
        int* alice_rank = (int*)malloc(sizeof(int) * player_count);
        *result_count = 0;
        alice_rank[0] = 1;
        
        for (int i=0; i<player_count; i++)
        {
            alice_rank[(*result_count)++] = binarySearch(ranked_opt, len, player[i]);
        }
        
        return alice_rank;
    }
    
  • + 0 comments

    For anyone trying to figure out the timeouts with TypeScript, I had to change all of my loops (including maps, reduces, and forEaches) to traditional for-loops. I believe the traditional for loop is more effiecient than the built in functions, so that is what got mine to work, particularly for test case number 6.

    function climbingLeaderboard(ranked: number[], player: number[]): number[] {
        let rank = 0;
        
        let lastRankedScore = -1;
        let rankedSet = [];
        
        for (let i = 0; i < ranked.length; i++) {
            const score = ranked[i];
            if (score !== lastRankedScore) {
                rankedSet.push(score);
                lastRankedScore = score;
            }
        }
        
        const result = [];
        
        for (let j = 0; j < player.length; j++) {
            const score = player[j];
            if (rank === 1 || score >= rankedSet[0]) {
                rank = 1;
                result.push(rank);
                continue;
            }
            
            const last = rankedSet[rankedSet.length - 1];
            
            if (score === last) {
                rank = rankedSet.length;
                result.push(rank);
                continue;
            }
            
            if (score < last) {
                rank = rankedSet.length + 1;
                result.push(rank);
                continue;
            }
            
            rank = 1;
            let pushed = false;
            for (let i = 0; i < rankedSet.length; i++) {
                if (score >= rankedSet[i]) {
                    result.push(rank);
                    pushed = true;
                    break;
                }
                
                rank++;
            }
            
            if (!pushed) {
                result.push(rank);
            }
        }
        
        return result;
    }
    
  • + 0 comments

    You can assume that the list of scores from the player will always be in ascending order. It would be nice to know that as part of the criteria.

    As an alternative, you could probably turn the player scores into pairs with their array index, sort them ascending, and then map the ranks to the pair's index to ensure correct ordering even if scores are out of order. That would be a real world-ish scenario.