Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to solve Time exceeded problem in Best Time to buy stock simple leetcode question

I am solving the leetcode question of Best Time to buy stock:
I have submitted this code with one for loop and I have got time limit exceeded. I cannot figure out why.

class Solution(object):
def maxProfit(self, prices):
    maxPro = 0
    size = len(prices)
    for i in range(size-1,0,-1):
        minimum = min(prices[0:i])
        maxPro = max(prices[i]-minimum,maxPro)
    return maxPro

please help

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

From TimeComplexity min() and max() functions have O(n) complexity on List, so it is basically another loop. So your time complexity is O(n^2)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading