### mkmeden's blog

By mkmeden, history, 10 months ago,

Can some one help me undertand why this solution is giving a TLE for a particular test case.

Solution :

Code
• 0

 » 10 months ago, # |   0 If i understand your code correctly, you go with bfs and for each cell in the labyrinth you store the path from starting cell to this cell, and in the end you just print path of finishing cell. It will probably use too much memory/time. Instead of it, try to store it's "parent", i.e. the cell, from wich you come to your current cell. This way, you will be able to easily construct your path from finish cell to start, and then just reverse.
•  » » 10 months ago, # ^ | ← Rev. 2 →   0 Thank you for your response .But i cant understand why my approach would take more time. It does take a lot of memory storing the strings but why time actually. And how would your approach save this time and memory as we are storing the parents which would take memory.
•  » » » 10 months ago, # ^ |   0 Let's suppose we have 900x900 labyrinth with each cell unlocked, A is in point (0,0) and B is in point (899,899). Storing parents with single char will take 8*900*900=6480000 bits, storing path with your approach will take(let's suppose 900 is average length of string you store) 8*900*900*900=5832000000 bits. My calculations may be a little bit off, but I hope you get the general picture.
•  » » » 10 months ago, # ^ |   0 Firstly, you could have time and memory limit at the same time, but checker shows only TL because it happens first. Your time complexity in the worst case is O(n^2*log(n)*c) where c is a constant of all your computations, including map , char> finding time, string concatenation(i'm not sure about this, maybe it's even O(length)). O(10^8) ~ 1 second, and your time complexity is O(10^7*c), which is luckily too much
•  » » » » 10 months ago, # ^ |   0 n^2 in time complexity I understand. But the log(n) part??? also n^2 will give only 10^6 worst case. Why 10^7
•  » » » » 10 months ago, # ^ |   0 I understood the correct method, but still confused why my initial one didnt work.
•  » » » » » 10 months ago, # ^ |   0 Oh, i thought you were using priority queue instead of queue, but nevermind, it doesn't change anything else.At least, there is ML error.Also, I checked your code and it runs in 57 seconds.I don't know exactly how it works, probably huge amount of memory needs a lot of time to run.
•  » » » » » » 10 months ago, # ^ |   0 daymn...guess i ll have to avoid using this method anyway.. Thanks
•  » » » » » » » 10 months ago, # ^ |   0 Just stick with the standard BFS always. Store parents no paths
•  » » » » » » » » 10 months ago, # ^ |   0 ya