Easy Java Solution but O(n^2) timing :(


  • 0
    E
    public class Solution {
        public String[] findRelativeRanks(int[] nums) {
            String str[]=new String[nums.length];
            int arr[] = new int[nums.length];
    		int x = 0;
    		for (int i = 0; i < nums.length; i++) {
    			int max = nums[i], index = i;
    			for (int j = 0; j < nums.length; j++) {
    				if (max < nums[j]) {
    					max = nums[j];
    					index = j;
    				}
    
    			}
    			arr[index] = x++;
    			nums[index] = Integer.MIN_VALUE;
    		}
    		for(int i=0;i<arr.length;i++){
    		    if(arr[i]==0){
    		        str[i]="Gold Medal";
    		    }
    		    else if(arr[i]==1){
    		        str[i]="Silver Medal";
    		    }
    		    else if(arr[i]==2){
    		        str[i]="Bronze Medal";
    		    }
    		    else{
    		        arr[i]++;
    		        str[i]=arr[i]+"";
    		    }
    		}
    		return str;
        }
    }
    

Log in to reply
 

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