Sort by

recency

|

2353 Discussions

|

  • + 1 comment

    My Python code is unable to complete four testcases because it can't complete the task within the time limit. Can anyone hlep me optimize my code?

    def climbingLeaderboard(ranked, player):
        ranks = []
        for a in range(len(player)):
            ranked.append(player[a])
            ranked = list(reversed(sorted(list(set(ranked)))))
            ranks.append(ranked.index(player[a]) + 1)
        return ranks
    
  • + 0 comments

    Time complexity: O(n*log(n))

    Space complexity: O(n)

    Idea:

    1. Build the map to get the rank of each ranked_arr[i].
    2. Binary search to get the index of the player[i] insert into ranked_arr.
    3. Get the rank for player[i].
    int* build_ranked_arr(int length, int* arr){
        int rank = 1;
        int* res_arr = (int* )malloc(length*sizeof(int));
        res_arr[0] = rank;
        for (int i = 1; i<length; i++){
            if (arr[i] == arr[i-1]) res_arr[i] = res_arr[i-1];
            else{
                rank++;
                res_arr[i] = rank;
            } 
        }
        return res_arr;
    }
    
    int binary_search_find_left_index(int length, int* arr, int target){
        int l = 0, r = length-1;
        while (l <= r){
            int m = (l+r)/2;
            if (arr[m] > target) l = m+1;
            else r = m-1;
        }
        return l;
    }
     
    int* climbingLeaderboard(int ranked_count, int* ranked, int player_count, int* player, int* result_count) {
        int* ranked_arr = (int* )malloc(ranked_count*sizeof(int));
        int* res_arr = (int* )malloc(player_count*sizeof(int));
        *result_count = player_count;
        ranked_arr = build_ranked_arr(ranked_count, ranked);
        for (int i = 0; i<player_count; i++){
            int temp_index = binary_search_find_left_index(ranked_count, ranked, player[i]);
            if (temp_index == 0) res_arr[i] = 1;
            else{
                res_arr[i] = ranked_arr[temp_index-1]+1;
            }
        }
        return res_arr;
    }
    
  • + 0 comments

    std::vector climbingLeaderboard(std::vector rk, std::vector p) { std::vector uu; for (size_t i = 0; i < rk.size(); i++) { if (i == 0 || rk[i] != rk[i - 1]) { uu.push_back(rk[i]); } } std::vector rst(p.size()); int ri = uu.size() - 1; for (size_t i = 0; i < p.size(); i++) {

        while (ri >= 0 && p[i] >= uu[ri]) {
        ri--;
        }
        rst[i] = ri + 2;
    }
    
    return rst;
    

    }

  • + 0 comments
      var  poistion_array = []
    
    var poistion_dic = {}
    
    var poistion = 1
    for (var i = 0; i < ranked.length ; i++){
    
        if ( poistion_dic[ranked[i]] == undefined){
    
            poistion_dic[ranked[i]] = poistion
            poistion++
    
        }
    
    }
    
    var previous_index =  ranked.length - 1
    for (var i = 0; i < player.length; i++){
    
        var flag = true
        
    
        for (var j  = previous_index ; j >=0; j--){
              previous_index  = j
            if(player[i] < ranked[j]){
    
                 poistion_array.push(poistion_dic[ranked[j]] +1)
                 j = -1
                 flag = false
    
            }
    
    
            if(player[i] == ranked[j]){
    
                poistion_array.push(poistion_dic[ranked[j]])
                j = -1
                flag = false
    
            }
            
            
        previous_index 
    
    
    
        }
        if (flag ){
    
            poistion_array.push(1)
    
        }
    }
    console.log(poistion_array)
    return poistion_array
    }
    
  • + 0 comments

    var poistion_array = []

    var poistion_dic = {}

    var poistion = 1 for (var i = 0; i < ranked.length ; i++){

    if ( poistion_dic[ranked[i]] == undefined){
    
        poistion_dic[ranked[i]] = poistion
        poistion++
    
    }
    

    }

    var previous_index = ranked.length - 1 for (var i = 0; i < player.length; i++){

    var flag = true
    
    
    for (var j  = previous_index ; j >=0; j--){
          previous_index  = j
        if(player[i] < ranked[j]){
    
             poistion_array.push(poistion_dic[ranked[j]] +1)
             j = -1
             flag = false
    
        }
    
    
        if(player[i] == ranked[j]){
    
            poistion_array.push(poistion_dic[ranked[j]])
            j = -1
            flag = false
    
        }
    
    
    previous_index 
    
    
    
    }
    if (flag ){
    
        poistion_array.push(1)
    
    }
    

    } console.log(poistion_array) return poistion_array }