Full Coutning Sort timing out for n=1000000 JAVA

12 Views Asked by At

Trying to get my FullCoutning sort function but it is failing due to timing out when the input is very large ( size arr = 1000000)

public class Solution {
    static class Result {

        /*
         * Complete the 'countingSort' function below.
         *
         * The function is expected to return an INTEGER_ARRAY.
         * The function accepts INTEGER_ARRAY arr as parameter.
         */

        public static void countSort(List<List<String>> arr) {
          // Write your code here
          int halfSize = arr.size()/2;
          // List<Integer> arrElements = new ArrayList<>(Collections.nCopies(100))
          HashMap<Integer,StringBuilder> result = new HashMap<>();
          // List<List<String>> result = Stream.generate(ArrayList<String>::new).limit(100).collect(Collectors.toList());
          for (int index=0;index<arr.size();index++){
            // String number = arr.get(index).get(0);
            StringBuilder word = new StringBuilder(arr.get(index).get(1));
            int pos = Integer.parseInt(arr.get(index).get(0));
            // StringBuilder word = new StringBuilder(" ");
            if (index < halfSize) word = new StringBuilder("-");
            if (!result.containsKey(pos)){
              result.put(pos,new StringBuilder()); 
            }         
            StringBuilder appendWord = word.append(" ");
            result.get(pos).append(appendWord);
            
          } 
          // result.forEach(s -> System.out.println(s));
          // System.out.println(result.stream().map(item -> String.join(" ", item)).collect(Collectors.joining(" ")).trim());
        
          for (Integer index: result.keySet()){
            String sentence = result.get(index).toString();
            System.out.print(sentence);
      }      
          return ;

        }

    }

    public static void main(String[] args) throws IOException {
      BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
      List<List<String>> arr = new ArrayList<>();
      
      int n = Integer.parseInt(bufferedReader.readLine().trim());
      IntStream.range(0,n).forEach(i -> {
      
        try {
          arr.add(Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
          .collect(toList())
        );
        } catch (IOException e) {
          // TODO: handle exception
          throw new RuntimeException();

        
        }

      });


      Result.countSort(arr);


      bufferedReader.close();
    }
}

I believe this is due to long time for reading input. Any suggestions ?

0

There are 0 best solutions below