• + 0 comments

    (solution in Javascript(TS))i tracked where the last index was when the rank was updated and started the next loop from that index :

    also i think the logic for comparing if the player's top rank [at last index] is bigger than ranked's higest could use some work, i would really love some suggestions for this .

    function climbingLeaderboard(ranked: number[], player: number[]): number[] {
      const rankedSet = Array.from(new Set(ranked)).reverse();
      const playerRank: number[] = [];
    
      let lastIndex = 0;
    
      player.forEach((score) => {
        for (let i = lastIndex; i < rankedSet.length; i++) {
          if (score < rankedSet[i]) {
            playerRank.push(rankedSet.length - i + 1);
            lastIndex = i;
            break;
          } else if (i === rankedSet.length - 1) {
            playerRank.push(1);
          }
        }
      });
    
      return playerRank;
    }