Sort by

recency

|

2375 Discussions

|

  • + 0 comments

    I believe this is the most optimized way to do it. Maybe we can remove the sort parts but it is still efficient. The key point is: the parameters are already sorted. Basically, it is possible to iterate through player list and keep the last compared index to not iterate all ranked array repeatedly.

    def climbingLeaderboard(ranked, player):
        ranked = list(set(ranked))
        scores = []
        length = len(ranked)
        
        player.reverse() # We use reversed list, we basically want the ranked and player arrays in the same order.
        matched = {}
        current_rank = 0
        for i in player:
    
            if matched.get(i, None): # remove duplicated computations.
                scores.append(matched[i])
                continue
    
            if current_rank >= length: # in last elements, it is likely to rank as last. 
                scores.append(current_rank + 1)
                continue
    
            if i > ranked[current_rank] or i == ranked[current_rank]: # if score is higher than current rank, assign the rank.
                matched[i] = current_rank + 1
                scores.append(current_rank + 1)
            else: # if it is lesser, we need an iteration since it is getting higher than current_rank. In short, we skip unnecessary parts. This is the optimization part.
                while current_rank < length and i < ranked[current_rank]:
                    current_rank += 1
                matched[i] = current_rank + 1
                scores.append(current_rank + 1)
        scores.reverse() # we have reversed the player list, so we need to reverse scores.
        print(scores)
        return scores
    
  • + 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;
    }
    
  • + 0 comments

    A longer C++ solution:

    vector<int> climbingLeaderboard(vector<int> ranked, vector<int> player) {
        vector<int> result;
        size_t r_ind = 0;
        int place = 0;
        result.resize(player.size());
    
        for (size_t p_ind = player.size(); p_ind-- > 0;) {
            int old_ranked = -1;
            while (r_ind < ranked.size() && ranked[r_ind] > player[p_ind]) {
                if (old_ranked != ranked[r_ind]) {
                    place++;
                }
                old_ranked = ranked[r_ind];
                r_ind++;
            }
            result[p_ind]= place + 1;
        }
        return result;
    }
    
  • + 0 comments

    Here is problem solution in Python, Java, C++, C and Javascript - https://programmingoneonone.com/hackerrank-climbing-the-leaderboard-problem-solution.html

  • + 0 comments

    Climbing the leaderboard in anything takes passion, consistency, and a bit of strategy — and finding the Best Apartment Communities is no different. Whether you’re new in town or just looking for an upgrade, it feels like a game where every move counts. From location and amenities to that perfect floor plan, every little detail adds up. It’s kind of exciting to tour places and see where you’ll rank them on your own list. I’ve definitely had those this is it moments walking into a new space. The Best Apartment Communities make it easy to feel at home while still feeling like you're winning. Have you checked out any recently that you’d rate a 10/10?