Simple code by Stream API


  • 0
    X
    public String[] findRelativeRanks(int[] nums) {
        final String[] namedRanks = {"Gold Medal", "Silver Medal", "Bronze Medal"};
        final int n = nums.length;
        final int[] indics = IntStream.range(0, n).boxed().sorted((i, j) -> nums[j] - nums[i]).mapToInt(i -> i).toArray();
        final String[] res = new String[n];
    
        for (int i = 0; i < n; i++) {
            res[indics[i]] = i < namedRanks.length ? namedRanks[i] : String.valueOf(i + 1);
        }
    
        return res;
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.