Why there is run time error for my code?


  • 0
    Q

    Any suggestion is appreciated!

       class Solution:
            # @param prices, a list of integer
            # @return an integer
            def maxProfit(self, prices,search_buy=True,search_sell=False,buy_price=0,profit=0,i=0):
                n=len(prices)
                if n==0:
                    return 0
                    
                if i==n-1:
                    if search_sell==True:
                        profit=profit+prices[n-1]-buy_price
                    return profit
                    
                if prices[i] < prices[i+1]:
                    if search_buy==True:
                        buy_price=prices[i]
                        search_buy=False
                        search_sell=True
                                
                elif prices[i] > prices[i+1]:
                    if search_sell==True:
                        sell_price=prices[i]
                        search_buy=True
                        search_sell=False
                        profit=profit+sell_price-buy_price
                i=i+1
                self.maxProfit(prices,search_buy,search_sell,buy_price,profit,i)

Log in to reply
 

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