It keeps telling me there is a runtime error, with prices[0] index out of range... But it runs on my machine.


  • 0
    M
    class Solution:
        # @param prices, a list of integer
        # @return an integer
        def maxProfit(self, prices):
            profit = 0
            min = prices[0]
    
            for i in range(1, len(prices), 1):
                # update max profit
                profitSellingItAtTimei = prices[i] - min
                profit = profitSellingItAtTimei if profitSellingItAtTimei > profit else profit
                # update min price
                min = prices[i] if prices[i] < min else min
    
            return profit
    

    It keeps telling me there is a runtime error, with prices[0] index out of range... But it runs on my machine. Any thoughts?


  • 1
    S

    What if prices is an empty list?

    min = prices[0]
    

    should cause the error.


  • 0
    M

    Oops yea I forgot about the edge cases! Thanks!


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.