LeetCode 2175: The Change in Global Rankings Solution

Master LeetCode problem 2175 (The Change in Global Rankings), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2175. The Change in Global Rankings

Medium

Problem Explanation

Explanation

To solve this problem, we can use a hash map to store the initial rankings of the players and the changes in their rankings. Then, we can sort the players based on their final rankings and return the final rankings in the order of the input.

  1. Create a hash map to store the initial rankings of the players.
  2. Iterate over the changes array and update the rankings in the hash map accordingly.
  3. Sort the players based on their final rankings.
  4. Return the final rankings in the order of the input.

Time complexity: O(n log n) where n is the number of players Space complexity: O(n) for the hash map

Solution Code

import java.util.*;

class Solution {
    public int[] findRelativeRanks(int[] nums) {
        Map<Integer, Integer> initialRankings = new HashMap<>();
        int n = nums.length;
        
        for (int i = 0; i < n; i++) {
            initialRankings.put(nums[i], i);
        }
        
        Arrays.sort(nums);
        
        String[] medals = {"Gold Medal", "Silver Medal", "Bronze Medal"};
        for (int i = 0; i < n; i++) {
            if (n - i <= 3) {
                nums[i] = medals[n - i - 1];
            } else {
                nums[i] = String.valueOf(n - i);
            }
        }
        
        int[] result = new int[n];
        for (int i = 0; i < n; i++) {
            result[initialRankings.get(nums[i])] = i + 1;
        }
        
        return result;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2175 (The Change in Global Rankings)?

This page provides optimized solutions for LeetCode problem 2175 (The Change in Global Rankings) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2175 (The Change in Global Rankings)?

The time complexity for LeetCode 2175 (The Change in Global Rankings) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2175 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2175 in Java, C++, or Python.

Back to LeetCode Solutions