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.
Swift: No spoilers of the solution here, I just give some tips on how to optimise reading the input in Swift.
Reading input and general string manipulation in Swift can be pretty slow if you use the standard/normal techniques. You may struggle with Test Case #4 and almost certainly will time out on #5.
So, for Test Case #5...
I tried various experiments and reading the input alone takes 0.48s with this code:
...with no other code times out (>2s at time of writing). So unfortunately just getting the input is too slow using this technique. Another way to process the line string into an Int and String is this:
This should be enough for you to solve the rest by yourself, but I will just point out that it took 1.41s to read the input plus the processing for the algorithm alone (no output) using the above code. My final solution took just 1.16s because I optimised away unnecessary code from the withCString closure above as well. Without that optimisation you will be sailing pretty close to the 2s time-out.
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 →
Swift: No spoilers of the solution here, I just give some tips on how to optimise reading the input in Swift.
Reading input and general string manipulation in Swift can be pretty slow if you use the standard/normal techniques. You may struggle with Test Case #4 and almost certainly will time out on #5.
So, for Test Case #5...
I tried various experiments and reading the input alone takes 0.48s with this code:
Adding in the standard "Foundation" way to process this into an
Int
and aString
, like this:...with no other code times out (>2s at time of writing). So unfortunately just getting the input is too slow using this technique. Another way to process the
line
string into anInt
andString
is this:...and this now takes 1.09s.
This should be enough for you to solve the rest by yourself, but I will just point out that it took 1.41s to read the input plus the processing for the algorithm alone (no output) using the above code. My final solution took just 1.16s because I optimised away unnecessary code from the
withCString
closure above as well. Without that optimisation you will be sailing pretty close to the 2s time-out.