Things to consider to solve the TLE


So, I’ve spent a lot of time to solve this problem… basically, to solve the TLE, not the problem itself. So, after you solve it, consider two things for your final solution:

  1. Do not use ArrayList, LinkedList or Sets (HashMap or HashSet)… use only arrays like int[]
  2. Do not store strings in the queue, use only ints and build your string at the end.


Thanks, the first suggestion really helped. The time bound in this problem is way too tight in my opinion, and I was using hashmap with constant access time, which is supposed to save memory, but was getting TLE no matter what. Once I switched to a two dimensional array, it was accepted.


I’m glad to be of help :slightly_smiling_face:


thanks a lot bro :slight_smile: