climbingLeaderboard.js

    0

    0

    This code retrieves the best rank for a given score from a sorted list of unique scores. The ranks are returned as an array of integers.

    function climbingLeaderboard(scores, alice) {
      let result = [];
      let uniqueScores = [...new Set(scores)];
    for (const score of alice) {
        if (score >= uniqueScores[0]) {
          result.push(1);
        } else if (score < uniqueScores[uniqueScores.length - 1]) {
          result.push(uniqueScores.length + 1);
        } else {
          result.push(rankBinarySearch(score, uniqueScores));
        }
      }
      return result;
    }
    function rankBinarySearch(score, uniqueScores) {
      let start = 0;
      let end = uniqueScores.length - 1;
    while (true) {
        let mid = Math.floor((start + end) / 2);
    // base cases
        if (uniqueScores[mid] === score) {
          return mid + 1;
        } else if (uniqueScores[mid] > score && uniqueScores[mid + 1] < score) {
          return mid + 2;
        } else if (uniqueScores[mid] < score && uniqueScores[mid - 1 > score]) {
          return mid - 1;
        }
    // recursion
        if (score < uniqueScores[mid]) {
          start = mid + 1;
        } else {
          end = mid - 1;
        }
      }
    }
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.