So for this program the user is supposed to input two numbers on the first line separated by a space. The first number is the number of contestants in a competition we will call it M and the second number is the total amount of votes received N.
The user then inputs M lines consisting of a name and a country Then the user inputs N lines consisting of only a name
The goal of the program is to calculate the name that got the most number of votes as well as the country and print them out. If there is a tie for the most number of votes you print out the lexicographically smallest.
I have what I believe to be as working solution to this problem but when I try to submit my code I get wrong answer. Even though it works for all given test cases and test cases that I created on my own.
Example I/O: Input: 4 5 Ramanujan India Torricelli Italy Gauss Germany Lagrange Italy Ramanujan Torricelli Torricelli Ramanujan Lagrange
Output: Italy Ramanujan
my code:
public class ChefOfTheYear2 {
static PrintWriter pw = new PrintWriter(System.out, true);
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String contestants[]= (br.readLine()).split(" ");
ArrayList<String> allC = new ArrayList<String>();
ArrayList<C> allC2 = new ArrayList<C>();
String[] input = new String[2];
Map<String, C> countries = new HashMap<String, C>();
Map<String, Contestant> people = new HashMap<String, Contestant>();
String[] keys = new String[Integer.parseInt(contestants[0])];
//add countries being voted on into allC array
for(int i=0; i<Integer.parseInt(contestants[0]); i++)
{
input = br.readLine().split(" ");
if(!allC.contains(input[1]))
allC.add(input[1]);
people.put(input[0], new Contestant(input[0], input[1]));
keys[i]=input[0];
}
// Creates arrayList of Country objects
for(int i=0; i<allC.size(); i++)
{
allC2.add(new C(allC.get(i)));
}
//creates hashmap of country objects and the key is the contestants name who is from that country
for(int i=0; i<Integer.parseInt(contestants[0]); i++)
{
countries.put(keys[i],allC2.get(search(people.get(keys[i]).getCountry(), allC)));
}
//amount of votes received
int totalVotes = Integer.parseInt(contestants[1]);
for(int i=0; i<totalVotes; i++)
{
String newKey= br.readLine();
people.get(newKey).incVotes();
countries.get(newKey).incVotes();
}
//Sorts the arrays
C[] updatedC = countries.values().toArray(new C[countries.size()]);
Arrays.sort(updatedC, new CountryComparator());
Contestant[] updatedPeople = people.values().toArray(new Contestant[people.size()]);
Arrays.sort(updatedPeople, new peopleComparator());
pw.println(updatedC[updatedC.length-1].getName());
pw.println(updatedPeople[updatedPeople.length-1].getName());
pw.println("Bam! DOne! "+ System.currentTimeMillis());
}
public static int search(String key, ArrayList<String> a)
{
for(int i=0; i<a.size(); i++)
{
if(a.get(i).equalsIgnoreCase(key))
return i;
}
return -1;
}
}
class C
{
int votes=0;
String name="";
public C(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
public void incVotes()
{
votes++;
}
public int getVotes()
{
return votes;
}
}
class Contestant
{
String country="";
int votes=0;
String name="";
public Contestant(String name, String country)
{
this.name=name;
this.country=country;
}
public String getCountry() {
return country;
}
public String getName()
{
return name;
}
public void incVotes()
{
votes++;
}
public int getVotes()
{
return votes;
}
}
class CountryComparator implements Comparator<C>
{
@Override
public int compare(C a, C b) {
int avote= a.getVotes();
int bvote= b.getVotes();
return avote>bvote ?1 : bvote>avote ?-1 : b.getName().compareTo(a.getName());
}
}
class peopleComparator implements Comparator<Contestant>
{
@Override
public int compare(Contestant o1, Contestant o2) {
int avote= o1.getVotes();
int bvote= o2.getVotes();
return avote>bvote ?1 : bvote>avote ?-1 : o2.getName().compareTo(o1.getName());
}
pw.println("Bam! DOne! "+ System.currentTimeMillis());? – tb- Jan 7 at 21:12