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.
There's a few problems that will cause you to time out.
copying the entire array is costly
printing individual values to stdout is costly
I was having a similiar problem, I solved #2 by building the output string up with "\n".join(arr) then printing it all out at once. The speed difference is dramatic.
I was still timing out on a few. after looking up a few things, I realized, I needed to sort in stages.
First I sorted by length, using a dictionary, and kept the values as strings, then I sorted those string lists lexically.
Big Sorting
You are viewing a single comment's thread. Return to all comments →
There's a few problems that will cause you to time out.
I was having a similiar problem, I solved #2 by building the output string up with
"\n".join(arr)
then printing it all out at once. The speed difference is dramatic. I was still timing out on a few. after looking up a few things, I realized, I needed to sort in stages. First I sorted by length, using a dictionary, and kept the values as strings, then I sorted those string lists lexically.Came out like this:
Which, as I read more comments is apparently a bucket sort?