Getting TLE - Please Help!!!


#1

Problem:- DIGJUMP

Please help me in optimizing this code which uses BFS to find the shortest distance between start and end vertices.

My code:- Solution


#2

I think your graph construction is wrong .
Lets say
2 2 2 2

So dig[2]={1,2,3,4}
So you do this:
For i=1:
Adj[1]=2,3,4
Adj[2]=1
Adj[3]=1
Adj[4]=1

For i=2
Adj[1]={2,3,4,2}
Adj[2]={1,1,3,4}

Hope it helps.correct me if i am wrong.


#3

But it is giving correct output for all the test cases. Any test cases where it fails?


#4

I am not saying it wont pass.

I am saying that due to this u might be getting tle.
Consider 2 2 2… 10^5 times

So your building graph itself takes O(N^2)

I would suggest try to build the graph properly


#5

Ohh Okay. I will try to implement it differently.