Using sort and binary search Java


  • 0
    Y
    class Solution {
        public String[] findRelativeRanks(int[] nums) {
            int[] n = new int[nums.length];
            for (int i = 0; i < nums.length; i++) {
                n[i] = nums[i];
            }
            Arrays.sort(n);
            
            String[] s = new String[nums.length];
            for (int i = 0; i < nums.length; i++) {
                int tmp = nums.length - Arrays.binarySearch(n, nums[i]);
                switch(tmp) {
                    case 1:
                        s[i] = "Gold Medal";
                        break;
                    case 2:
                        s[i] = "Silver Medal";
                        break;
                    case 3:
                        s[i] = "Bronze Medal";
                        break;
                    default:
                        s[i] = tmp + "";
                }
            }
            
            return s;
        }
    }

Log in to reply
 

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