geekcomputers / Python

My Python Examples
http://www.thegeekblog.co.uk
MIT License
30.91k stars 12.14k forks source link

How can I improve the complexity of a python program ? #1480

Open prateeekgoel7248 opened 2 years ago

prateeekgoel7248 commented 2 years ago

Hi! I do coding on many platforms and I got TLE(time limit exceed) errors many times so I just want to know how can I reduce the time complexity of my python program.

sankalp-sde commented 2 years ago

Can you please share the question?

dubeyji10 commented 2 years ago

@jha00 is right it depends on question for an example - recursive and overlapping sub problems are modified by memoization ( dynamic programming ) maybe a question requires you a better approach improving complexity is just about making solutions efficient ( btw i suck at this thing i am also a victim of TLE )

SohamRatnaparkhi commented 2 years ago

Usually in CP, TLE occurs when your code takes more than 5 sec. to run the test-case for Python code and 1sec for C/Cpp (in codeforces - 1sec for Python as well). You need to have a brief understanding of time complexity for this. By calculating the time complexity of an algorithm, it is possible to check, before implementing the algorithm, that it is efficient enough for the problem. image

Anyways, I too get the TLE occasionally😅😁

darthdaenerys commented 2 years ago

Well, to reduce the complexity of a program you have to use a better algorithm or sometimes an efficient data structure. Before writing the actual code, calculate the complexity and if its within the time constraints of question, then you're good to go otherwise try to find more efficient solution, optimize your code etc.