harry
BAN USER
To give another example for service,
MySql or particularly any database starts a service (at startup also) and we can use that service in our programs to do database operations. Unless the service is up, we will not be able to run any JDBC connection required program.
When an exception occurs and if re-throwing the same exception, then stackTrace would be still maintained. Unless we create a new exception object, the stack trace of the exception from callee method still preserves.
public class RethrowException {
void method1() throws Exception{
throw new Exception("exception message");
}
void method2() throws Exception{
try{
method1();
}catch(Exception exception){
throw exception;
}
}
public static void main(String[] args) throws Exception{
RethrowException re = new RethrowException();
re.method2();
}
}
A simple Brute-force approach to solve this problem and the complexity of this solution is O(n*m)
public class Stalls {
public static void main(String[] args) throws IOException {
//1. Take input from console
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//2. Get row1 and row2
String row1 = br.readLine();
String row2 = br.readLine();
//3. Validation of input rows
if(row1 == null || row1=="" || row2==null || row2==""){
System.out.println("Exiting as invalid inputs");
System.exit(0);
}
//4. Split the row1 elements with space and validate
String[] tokens1 = row1.split(" ");
if(tokens1==null || tokens1.length!=2){
System.out.println("Exiting as invalid input for row1");
System.exit(0);
}
//5. Capture the number of stalls and number of Tickets to be sold in two variables.
int numOfStalls = Integer.parseInt(tokens1[0]);
int numOfTickets = Integer.parseInt(tokens1[1]);
//6. Split the second row and validate against the number of stalls given in first row.
String[] tokens2 = row2.split(" ");
if(tokens2==null || tokens2.length!=numOfStalls){
System.out.println("Exiting as invalid input for row2");
System.exit(0);
}
//7. Find the maximum sum
findMaximum(numOfStalls, numOfTickets, tokens2);
//8. With Sorting
findMaximumMinComplexity(numOfStalls, numOfTickets, tokens2);
}
/**
* Find the maximum sum from different stalls.Complexity is O(n*m)
* @param numOfStalls
* @param numOfTickets
* @param tokens
*/
private static void findMaximum(int numOfStalls, int numOfTickets, String[] tokens) {
//1. Get the initial number of tickets in each stall in an ArrayList and initialize the values list and stall list.
List<String> ticketsInEachStall = new ArrayList<String>(Arrays.asList(tokens));
List<Integer> valueList = new ArrayList<Integer>();
List<Integer> stallList = new ArrayList<Integer>();
//2. Variable to capture the maximum sum.
int sum = 0;
//3. Variable to capture which stall ticket number has to be decreased.
int indexToDecrease = 0;
//4. For the number of tickets to be sold, loop and find the max value.
for(int ticketIndex=1;ticketIndex<=numOfTickets; ticketIndex++){
int maxValue = 0;
for(int stallIndex=0; stallIndex<ticketsInEachStall.size(); stallIndex++){
int ticketValueOfStall = Integer.parseInt(ticketsInEachStall.get(stallIndex));
if(ticketValueOfStall > maxValue){
indexToDecrease=stallIndex;
maxValue = ticketValueOfStall;
}
}
valueList.add(maxValue);
sum+=maxValue;
stallList.add(indexToDecrease+1);
int existingval = Integer.parseInt(ticketsInEachStall.get(indexToDecrease));
existingval--;
ticketsInEachStall.remove(indexToDecrease);
ticketsInEachStall.add(indexToDecrease, String.valueOf(existingval));
}
System.out.println("The maximum profit is $"+ sum);
System.out.println("Values list: "+ valueList);
System.out.println("------------------");
System.out.println("Stall number from which ticket has to be sold in order is : "+ stallList);
}
I think the simple code might be
- harry November 30, 2015