Simple solution using Sorting & Binary Search


  • 0
    B
    public class Solution {
      public String[] findRelativeRanks(int[] nums) {
        int[] ranked = nums.clone();
        Arrays.sort(ranked);
    
        String[] result = new String[nums.length];
        for (int i = 0; i < nums.length; ++i) {
          int rank = nums.length - Arrays.binarySearch(ranked, nums[i]);
          if (rank == 1) {
            result[i] = "Gold Medal";
          } else if (rank == 2) {
            result[i] = "Silver Medal";
          } else if (rank == 3) {
            result[i] = "Bronze Medal";
          } else {
            result[i] = String.valueOf(rank);
          }
        }
        return result;
      }
    }
    

Log in to reply
 

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