• + 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;
    }