www.fatihkabakci.com

Personal Website and Computer Science TUR EN

Interview Question Relative Ranks

Last update: 7/2/2017 8:30:00 PM

By Fatih KABAKCI

The problem is to figure out top highest scores of athletes who will be awarded Gold, Silver and Bronze. The final positions of them will be printed for others.

Problem #506 - Relative Ranks

Description: Given scores of N athletes, find their relative ranks and the people with the top three highest scores, who will be awarded medals: "Gold Medal", "Silver Medal" and "Bronze Medal".

Example 1:
Input: [5, 4, 3, 2, 1]
Output: ["Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"]
Explanation: The first three athletes got the top three highest scores, so they got "Gold Medal", "Silver Medal" and "Bronze Medal".
For the left two athletes, you just need to output their relative ranks according to their scores.

Note:
N is a positive integer and won't exceed 10,000.
All the scores of athletes are guaranteed to be unique.

Difficulty: Easy

Solution in Java

package com.fatihkabakci.Easy.RelativeRanks;

import java.util.Arrays;

/**
 * 
 * @author fkabakci
 * Problem Description: Given scores of N athletes, find their relative ranks and the people 
 * with the top three highest scores, who will be awarded medals: 
 * "Gold Medal", "Silver Medal" and "Bronze Medal".
 * 
 * Example 1:
 * Input: [5, 4, 3, 2, 1]
 * Output: ["Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"]
 * Explanation: The first three athletes got the top three highest scores, 
 * so they got "Gold Medal", "Silver Medal" and "Bronze Medal". 
 * For the left two athletes, you just need to output their relative ranks according to their scores.
 * 
 * Note:
 * 1. N is a positive integer and won"t exceed 10,000.
 * 2. All the scores of athletes are guaranteed to be unique.
 * 
 * Solution:
 * 1. Make a copy then sort the array.
 * 2. Find out indexes of elements in sorted array by using elements from the original array.
 * 3. Specify if it is gold, silver, bronze or other.
 */
public class Solution {
	private int index(int nums[], int num) {
		for(int i = 0; i < nums.length; i++)
			if(nums[i] == num)
				return i;
		return -1;
	}
	
	public String[] findRelativeRanks(int[] nums) {
		int length = nums.length;
		int[] nums2 = Arrays.copyOf(nums, nums.length);
		// ascending sort
		Arrays.sort(nums2);
		String[] scores = new String[length];
		
		for(int i = 0; i < nums.length; i++) {
			// descending index
			int index = length - 1 - index(nums2, nums[i]);
			switch (index) {
			case 0:
				scores[i] = "Gold Medal";
				break;
			case 1:
				scores[i] = "Silver Medal";
				break;
			case 2:
				scores[i] = "Bronze Medal";
				break;
			default:
				scores[i] = "" + (index + 1);
			}
		}
		return scores;
    }

	public static void main(String[] args) {
	}
}

There has been no comment yet

Name:


Question/Comment
   Please verify the image




The Topics in Computer Science

Search this site for





 

Software & Algorithms

icon

In mathematics and computer science, an algorithm is a step-by-step procedure for calculations. Algorithms are used for calculation, data processing, and automated reasoning.

Programming Languages

icon

A programming language is a formal constructed language designed to communicate instructions to a machine, particularly a computer. It can be used to create programs to control the behavior of a machine. Java,C, C++,C#

Database

icon

A database is an organized collection of data. The data are typically organized to model aspects of reality in a way that supports processes requiring information.

Hardware

icon

Computer hardware is the collection of physical elements that constitutes a computer system. Computer hardware refers to the physical parts or components of a computer such as the monitor, memory, cpu.

Web Technologies

icon

Web development is a broad term for the work involved in developing a web site for the Internet or an intranet. Html,Css,JavaScript,ASP.Net,PHP are one of the most popular technologies. J2EE,Spring Boot, Servlet, JSP,JSF, ASP

Mobile Technologies

icon

Mobile application development is the process by which application software is developed for low-power handheld devices, such as personal digital assistants, enterprise digital assistants or mobile phones. J2ME

Network

icon

A computer network or data network is a telecommunications network that allows computers to exchange data. In computer networks, networked computing devices pass data to each other along data connections.

Operating Systems

icon

An operating system is software that manages computer hardware and software resources and provides common services for computer programs. The OS is an essential component of the system software in a computer system. Linux,Windows

Computer Science

icon

Computer science is the scientific and practical approach to computation and its applications.A computer scientist specializes in the theory of computation and the design of computational systems.