Home python Time Limit Exceeded. Python 3.x

Time Limit Exceeded. Python 3.x




I decide it means TASK on Stepic. And I had a problem. It seems like the simplest task, wrote a code that, with my checking on random values, it issues everything normally. But Stepic gives an error Failed Test # 3. Time Limit Exceeded. Maybe someone disassembled with this task, or the question will be able to understand what’s the matter. The code will attach below just in case

a = {}
R = INT (INPUT ())
For i in Range (R):
  x = int (input ())
  A [x] = f (x)
  Print (A [x])

Answer 1, Authority 100%

Under the conditions of the task, the F (X) function is calculated for a very long time to pass the test you need to cache the result of execution and if the x value of the x that has already been calculated, you need to take value from the cache.

You cache resernote, but do not use a zashed response.

Answer 2

Consider that the function f (x) is already defined above. It is not necessary to determine it separately.

d = {}
For i in Range (int (Input ())):
  k = int (input ())
  if k not in d:
    D [k] = f (k)
  Print (D [K])

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions