We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Performance Improvements:
- used BufferedReader instead of Scanner
- used StringBuffer to do 1 output to console instead of multiple outputs
This code takes 1.11s to pass Testcase #5
importjava.io.BufferedReader;importjava.io.InputStreamReader;importjava.io.IOException;importjava.util.HashMap;importjava.util.ArrayList;publicclassSolution{publicstaticvoidmain(String[]args)throwsIOException{finalintmaxValue=100;/* Create HashMap with empty "buckets" to put Strings into */HashMap<Integer,ArrayList<String>>map=newHashMap<>(maxValue);for(inti=0;i<maxValue;i++){map.put(i,newArrayList<String>());}/* Save input */BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));intn=Integer.parseInt(br.readLine());for(inti=0;i<n;i++){String[]pair=br.readLine().split(" ");intkey=Integer.parseInt(pair[0]);Stringvalue=(i<n/2)?"-":pair[1];ArrayList<String>list=map.get(key);list.add(value);}br.close();/* Print output */StringBuffersb=newStringBuffer();for(inti=0;i<maxValue;i++){ArrayList<String>values=map.get(i);for(Stringstr:values){sb.append(str+" ");}}System.out.println(sb);}}
Let me know if you have any questions.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
The Full Counting Sort
You are viewing a single comment's thread. Return to all comments →
Java solution - passes 100% of test cases
From my HackerRank solutions.
Performance Improvements:
- used BufferedReader instead of Scanner
- used StringBuffer to do 1 output to console instead of multiple outputs
This code takes 1.11s to pass Testcase #5
Let me know if you have any questions.