Sunday, July 13, 2008

TopCoder Problem

http://www.topcoder.com/stat?c=problem_statement&pm=2922&rd=5855

Problem Statement


The Olympic Games in Athens end tomorrow. Given the results of the olympic disciplines, generate and return the medal table.



The results of the disciplines are given as a String[] results, where each element is in the format "GGG SSS BBB". GGG, SSS and BBB are the 3-letter country codes (three capital letters from 'A' to 'Z') of the countries winning the gold, silver and bronze medal, respectively.



The medal table is a String[] with an element for each country appearing in results. Each element has to be in the format "CCO G S B" (quotes for clarity), where G, S and B are the number of gold, silver and bronze medals won by country CCO, e.g. "AUT 1 4 1". The numbers should not have any extra leading zeros.

Sort the elements by the number of gold medals won in decreasing order. If several countries are tied, sort the tied countries by the number of silver medals won in decreasing order. If some countries are still tied, sort the tied countries by the number of bronze medals won in decreasing order. If a tie still remains, sort the tied countries by their 3-letter code in ascending alphabetical order.

Definition


Class:MedalTable
Method:generate
Parameters:String[]
Returns:String[]
Method signature:String[] generate(String[] results)
(be sure your method is public)



Constraints

-results contains between 1 and 50 elements, inclusive.
-Each element of results is formatted as described in the problem statement.
-No more than 50 different countries appear in results.

Examples

0)

{"ITA JPN AUS", "KOR TPE UKR", "KOR KOR GBR", "KOR CHN TPE"}
Returns:
{ "KOR 3 1 0",
"ITA 1 0 0",
"TPE 0 1 1",
"CHN 0 1 0",
"JPN 0 1 0",
"AUS 0 0 1",
"GBR 0 0 1",
"UKR 0 0 1" }
These are the results of the archery competitions.
1)

{"USA AUT ROM"}
Returns: { "USA 1 0 0",  "AUT 0 1 0",  "ROM 0 0 1" }

2)

{"GER AUT SUI", "AUT SUI GER", "SUI GER AUT"}
Returns: { "AUT 1 1 1",  "GER 1 1 1",  "SUI 1 1 1" }




My Solution:

import java.util.*;

public class MedalTable{
public String[] generate(String[] results){
Map<String,Country> map = new HashMap<String,Country>();
for (int i=0; i<results.length; i++){
String[] cont = results[i].split(" ");
Country cy = map.get(cont[0])!=null? map.get(cont[0]):new Country(cont[0]);
cy.g=cy.g+1;
map.put(cont[0],cy);
Country cy1 = map.get(cont[1])!=null? map.get(cont[1]):new Country(cont[1]);
cy1.s=cy1.s+1;
map.put(cont[1],cy1);
Country cy2 = map.get(cont[2])!=null? map.get(cont[2]):new Country(cont[2]);
cy2.b=cy2.b+1;
map.put(cont[2],cy2);
}

List<Country> list = new ArrayList<Country>();
list.addAll(map.values());
Collections.sort(list);
Collections.reverse(list);
System.out.println(list);
String[] r = new String[list.size()];
for (int i=0;i<list.size();i++){
r[i]=list.get(0).toString();
}
return r;
}

public class Country implements Comparable<Country> {
public int g=0;
public int s=0;
public int b=0;

public String name;

public Country(String name) {
this.name = name;
}
public String toString(){
return name+" "+g+" "+s+" "+b;
}

public int compareTo(Country con){
if (this.g!=con.g) return this.g-con.g;
if (this.s!=con.s) return this.s-con.s;
if (this.b!=con.b) return this.b-con.b;
return con.name.compareTo(this.name);
}
}

public static void main(String[] args){

MedalTable mt = new MedalTable();
mt.generate(new String[]{"ITA JPN AUS", "KOR TPE UKR", "KOR KOR GBR", "KOR CHN TPE"});
mt.generate(new String[]{"USA AUT ROM"});
mt.generate(new String[]{"GER AUT SUI", "AUT SUI GER", "SUI GER AUT"});

}
}

No comments: