cd_mas7er's blog

By cd_mas7er, history, 5 years ago, In English

Hello,

problem :- link

I am finding a difficulty in understanding why does one of my solution pass and the other is giving tle although i think the time complexity of both the codes are same.

tle code — link

ac code — link

tle code — i have written a recursive function and i am using 2d dp where dp[i][j] means minimum cost of opening those locks where the bit is set in the binary representation of j using only 0 to i keys. Base case — if i==m , that is all the keys are taken into consideration if the binary representation of j does not have all the bits set to 1 , then all the locks are not opened , return infinity and if all the locks are opened then return 0.

ac code — i have just written an iterative function and taken 1d dp where dp[i] = minimum cost of opening those locks where bit is set in the binary representation of i.

the time complexity of TLE code at worst can go O(m*(2^n)) because this the dimension of the dp i am filling and ac code has time complexity as O(m*(2*n)). Since both the solution have same time complexity i don't know why one is gettiing tle and other is not.

Could anyone please tell me what i am doing wrong. Thanks in advance.

  • Vote: I like it
  • +4
  • Vote: I do not like it

| Write comment?
»
5 years ago, # |
Rev. 2   Vote: I like it +7 Vote: I do not like it

memset dp with -1, not inf, because sometimes the answer of dp[i][j] will be inf so it will go to the same state many times.