In a competitive programming competition, I got the following problem famously known as 'Shoemaker's Problem':
"A shoemaker has N orders from customers that he must execute. The shoemaker can work on only one job each day. For each job i, it takes Ti days for the shoemaker to finish the job, where Ti is an integer and (1 ≤ Ti ≤ 1000). For each day of delay before starting to work for the job i, shoemaker must pay a fine of Si (1 ≤ Si ≤ 10000) rupees. Your task is to help the shoemaker find the sequence in which to complete the jobs so that his total fine is minimized. If multiple solutions are possible, print the one that is lexicographically least (i.e., earliest in dictionary order)."
For the solution of this problem, I tried to minimize the Si/Ti and then arrange them in the array.
My solution in java is as follows:
import java.util.Arrays;
import java.util.Scanner;
class Order implements Comparable<Order>
{
int time;
int fine;
int index;
public static void main(String...s)
{
Scanner sc= new Scanner(System.in);
int numOfElements=sc.nextInt();
Order[] orders= new Order[numOfElements];
for(int i=0;i<numOfElements;i++)
{
Order o=new Order();
o.time=sc.nextInt();
o.fine=sc.nextInt();
o.index=i+1;
orders[i]=o;
}
Arrays.sort(orders);
for(Order o:orders)
{
System.out.println(o.index);
}
}
@Override
public int compareTo(Order o)
{
int b=this.fine*o.time;
int a =o.fine*this.time;
return a-b;
}
}
So after submitting the solution, I got a score of 30 out of 100.
--Now I am using already present Arrays.sort(), which according to the following question asked, uses Mergesort for sorting arrays of objects:
Why does Java's Arrays.sort method use two different sorting algorithms for different types?
My question is it that, did my approach failed on efficiency or my logic would have failed on large input.
Thanks in advance...