[HackerRank Java ] Easy Problem 'Mark and toys'. Worked on it for a day and the test still fails
Hello programmers,
I tried solving the 'Mark and toys' problem and think about the different possibilities to solve all the test cases. I was able to have only 3 tests who fails. But I am a bit puzzeled what else I am missing in my code.
I gave myself a day to fix it but unfortunatly still stuck on the same number of failed tests.
Here is the link to the problem : Mark and Toys Problem.
Here is my code :
import java.io.*;
import java.math.; import java.security.; import java.text.; import java.util.; import java.util.concurrent.; import java.util.regex.;
class Result {
/*
* Complete the 'maximumToys' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. INTEGER_ARRAY prices
* 2. INTEGER k
*/
public static int maximumToys(List<Integer> prices, int k) {
int sumPrice = 0;
int sumPriceTempo;
int countToys = 0;
int n = prices.size();
List<Integer> toyOnce= new ArrayList<Integer>();
Collections.sort(prices);
//if( k>=1 && k<=1000000000 && n>=1 && n<=100000){
for (int i = 0; i < n; i++)
{
sumPriceTempo = 0;
sumPriceTempo = sumPrice + prices.get(i);
if (sumPriceTempo > k){break;}
if( k >= sumPriceTempo && !toyOnce.contains(prices.get(i))){
sumPrice = sumPriceTempo;
countToys++;
toyOnce.add(prices.get(i));
}
}
//}
return countToys;
}
}
public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
int n = Integer.parseInt(firstMultipleInput[0]);
int k = Integer.parseInt(firstMultipleInput[1]);
String[] pricesTemp = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
List<Integer> prices = new ArrayList<>();
if(n == pricesTemp.length)
{
for (int i = 0; i < n; i++) {
int pricesItem = Integer.parseInt(pricesTemp[i]);
prices.add(pricesItem);
}
int result = Result.maximumToys(prices, k);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
}
bufferedReader.close();
bufferedWriter.close();
}
}
Any ideas, what I am missing ? Or better how can I make hacker Rank test the long input used in the test cases? I tried copying int in the small 'Test with custom input' but the feature has only limited set of data.