Skip to content
Advertisement

Timeout problem | Find the rank of the player with given array of scores

“An arcade game player wants to climb to the top of the leaderboard and track their ranking.” So the problem gives you two array (ranked and player). In the player array, you have the points of the player in every round and you have to check his rank based on his score on that round.

  • Highest score is ranked 1 and the same score means the same rank.

My problem -> It executes well until it gets over 200k inputs (that means in the case ranked array has +200k smt) I’ll leave here my code and one of the inputs that I get timeout. I thought I could remember the index I found the player input bigger than the ranked[x] and for the next round score it will start from there and go up. But I couldn’t figure out how to implement it.

'use strict';

const fs = require('fs');

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', function(inputStdin) {
    inputString += inputStdin;
});

process.stdin.on('end', function() {
    inputString = inputString.split('n');

    main();
});

function readLine() {
    return inputString[currentLine++];
}

/*
 * Complete the 'climbingLeaderboard' function below.
 *
 * The function is expected to return an INTEGER_ARRAY.
 * The function accepts following parameters:
 *  1. INTEGER_ARRAY ranked
 *  2. INTEGER_ARRAY player
 */

function climbingLeaderboard(ranked, player) {
    // Write your code here
 let rank=1;
 let array=[];
for (let a=0; a<player.length; a++){
    for(let b=0; b<ranked.length; b++){
        if (player[a]<ranked[ranked.length-1]){
            ranked.push(player[a]);
            
           break;}
        else if(player[a]>ranked[b] || player[a]==ranked[b]){
          ranked.splice(b, 0, player[a]);
           break;
        }
       
       
    }
    
    for(let k=0; k<ranked.length; k++){
        if(ranked[k]>ranked[k+1]){
          
            if(ranked[k]==player[a]){
                ranked.splice(k, 1);
                 array[a]=rank;
                    rank=1;
                    break; 
        }
        rank++;
            if(ranked[k+1]==player[a]){
                ranked.splice(ranked.length-1, 1);
                 array[a]=rank;
                    rank=1;
                    break; 
            }
        
        }
        else if (ranked[k]==player[a]){
            ranked.splice(k,1);
            array[a]=rank;
            rank=1;
            break;
        }
    }
  
}
return array;
}

function main() {
    const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

    const rankedCount = parseInt(readLine().trim(), 10);

    const ranked = readLine().replace(/s+$/g, '').split(' ').map(rankedTemp => parseInt(rankedTemp, 10));

    const playerCount = parseInt(readLine().trim(), 10);

    const player = readLine().replace(/s+$/g, '').split(' ').map(playerTemp => parseInt(playerTemp, 10));

    const result = climbingLeaderboard(ranked, player);

    ws.write(result.join('n') + 'n');

    ws.end();
}

test case: https://hr-testcases-us-east-1.s3.amazonaws.com/29530/input06.txt?AWSAccessKeyId=AKIAR6O7GJNX5DNFO3PV&Expires=1646331273&Signature=hgw8s3So8qewCgxf%2FX8%2B19jTbaI%3D&response-content-type=text%2Fplain

Advertisement

Answer

Your solution in O(n^2)

Rather than looping through every time for every entry, you can organize the data in an object, so you only need to loop through once.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement