Time Limit Exceeded


  • 0
    L

    How to optimize the time?

    int maxProfit(vector<int>& prices) {
        int max=0;
        if (!prices.empty())
        {
            for(int i=0;i<prices.size()-1;i++)
            {
                if(prices[i]>prices[i+1])
                    continue;
                else
                {
                    int j=i+1;
                    while(j<prices.size())
                    {
                        if(max<(prices[j]-prices[i]))
                                max=prices[j]-prices[i];
                        j++;
                    }
                }
            }
        }
        return max;
    }

Log in to reply
 

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