0

I'm currently practicing on a website where they have time constraints for each challenge. Having fixed everything I could and still failing the time constraint of 0.5 seconds, I believe the high calculation time has something to do with my usual code of assigning a numbers string (e.g. "1 2 3" from system.in) to a variable by converting it into an array. My question would be if that one liner can be improved or if there is some better version of the java scanner (java scanner is very time inefficient) or bufferedreader that can be used in javascript so that not every spaced numbers string has to be converted into an array and then mapped to ints. here is my code so far:

importPackage(java.io);
importPackage(java.lang);
 
let reader = new BufferedReader( new InputStreamReader(System['in']) );

let t = parseInt(reader.readLine());

for (let i = 0; i<t; i++){
    let v = reader.readLine().split(" ").map(function(i){return parseInt(i)});  //this here
    print(v[0] + v[2] === 180 ? "yes" : "no")
}
  • What runtime environment is that, Rhino/Nashorn? What website is that? This is really unusual JavaScript. – Bergi Jul 28 '22 at 23:50
  • Instead of a `BufferReader`, you might be looking for a [`Scanner`](https://stackoverflow.com/q/2506077/1048572). But this is all Java builtins, not JavaScript. – Bergi Jul 28 '22 at 23:54
  • @Bergi it's codechef.com running with rhino - I think the scanner can be imported to js as well but if I understood correctly it has a way higher time complexity than the bufferedreader, that's why i don't use it – stanleysearles Jul 29 '22 at 09:06
  • Why do you think it has non-linear time complexity? – Bergi Jul 29 '22 at 09:08
  • @Bergi idk just read that it is way slower in processing – stanleysearles Jul 31 '22 at 00:38
  • Where did you read that, do you have a link? Or a quote of what exactly you read? Either way, the only way to find out is [to race your horses](https://ericlippert.com/2012/12/17/performance-rant/). – Bergi Jul 31 '22 at 00:41
  • "*I believe the high calculation time has something to do with my usual code of assigning a numbers string from system.in to a variable by converting it into an array.*" - I don't believe that. Is this your whole code that takes more than half a second when running? Also how large is the input actually, really just three numbers? – Bergi Jul 31 '22 at 00:46

0 Answers0