Find the limit to the number of items that can be prepared with given amount of ingredients -
Let's try to understand with input/output pattern:
Input
4
2 5 6 3
20 40 90 50
Output
8
1st Line: Number Of Ingredients
2nd Line: Each unit required from total ingredients given
3rd Line: Each value represent total number of ingredients in our hand.
Explanation:
for each item:
2 units will be used from 20
5 units will be used from 40
6 units will be used from 90
3 units will be used from 50
until we run out of any ingredient.
After 8th attempt we are running out with ingredient 40, and hence maximum items can be prepared will be 8.
I have proposed the below solution, written in Java and it is working fine for all the test cases, but for some test cases it is taking 1 sec of time to execute, I want to improve the code, Please suggest me some best practices applicable which is helpful in order to optimize the code.
package com.mzk.poi;
import java.util.Collections;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class PowerPuffGirls {
private static final String SPACE = " ";
private static final Integer INITAL_IDX = 0;
private static final Integer LOWER_IDX = 1;
private static final Integer SECOND_IDX = 2;
private static final Integer MAX_LINES = 3;
private static final Integer UPPER_IDX = 10000000;
private static long[] quantityOfIngredients;
private static long[] quantityOfLabIngredients;
private static int size = 0;
private static long numberOfIngredients = 0;
/**
* This method will terminate the execution
*/
private static void terminate() {
System.exit(INITAL_IDX);
}
/**
* This method validated the input as per the specified range
*
* @param eachQunatity
* @return boolean
*/
private static boolean validateQuantityOfEachIngredients(long eachQunatity) {
return eachQunatity >= INITAL_IDX && eachQunatity > Long.MAX_VALUE ? true : false;
}
/**
* This helper method will parse the string and return long value
*
* @param input
* @return long
*/
private static long getNumberOfIngredients(String input) {
return Long.parseLong(input);
}
/**
* This method validates the first input
*
* @param noOfIngredients
* @return boolean
*/
private static boolean validateNumberOfIngredients(String noOfIngredients) {
numberOfIngredients = getNumberOfIngredients(noOfIngredients);
return numberOfIngredients >= LOWER_IDX && numberOfIngredients <= UPPER_IDX ? true : false;
}
/**
* This utility method convert the String array to Integer array
*
* @param size
* @param specifiedArrayOfUnits
* @return long[]
*/
private static long[] convertToLongArray(String[] arrayToBeParsed) throws NumberFormatException {
long array[] = new long[size];
for (int i = INITAL_IDX; i < size; i++) {
array[i] = Long.parseLong(arrayToBeParsed[i]);
}
return array;
}
public static void main(String[] args) {
String[] arrOfQuantityOfIngredients = null;
String[] arrOfQuantityOfLabIngredients = null;
Set<Long> maxPowerPuffGirlsCreationList = new HashSet<Long>();
Scanner stdin = new Scanner(System.in);
String[] input = new String[MAX_LINES];
try {
for (int i = 0; i < input.length; i++) {
input[i] = stdin.nextLine();
}
} finally {
stdin.close();
}
if (!validateNumberOfIngredients(input[INITAL_IDX])) {
terminate();
}
String quantityOfEachIngredients = input[LOWER_IDX];
String quantityOfEachLabIngredients = input[SECOND_IDX];
arrOfQuantityOfIngredients = quantityOfEachIngredients.split(SPACE);
arrOfQuantityOfLabIngredients = quantityOfEachLabIngredients.split(SPACE);
size = arrOfQuantityOfIngredients.length;
try {
quantityOfIngredients = convertToLongArray(arrOfQuantityOfIngredients);
for (int i = 0; i <= quantityOfIngredients.length - 1; i++) {
if (validateQuantityOfEachIngredients(quantityOfIngredients[i])) {
terminate();
}
}
quantityOfLabIngredients = convertToLongArray(arrOfQuantityOfLabIngredients);
for (int i = 0; i <= quantityOfLabIngredients.length - 1; i++) {
if (validateQuantityOfEachIngredients(quantityOfLabIngredients[i])) {
terminate();
}
}
for (int i = 0; i < numberOfIngredients; i++) {
long min = quantityOfLabIngredients[i] / quantityOfIngredients[i];
maxPowerPuffGirlsCreationList.add(quantityOfLabIngredients[i] / quantityOfIngredients[i]);
}
System.out.println(Collections.min(maxPowerPuffGirlsCreationList));
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}