Anyone can tell me why my time is exceeded?


  • 0
    S
    int left_min,right_min,l_max_profit=0,r_max_profit=0,max_profit,fianl_profit=0;
    	unsigned int totaldays = prices.size();
    	if (totaldays<1||totaldays==1)
    	{
    		return 0;
    	}
    	else if (totaldays==2)
    	{
    		fianl_profit = max(prices[0] - prices[1], prices[1] - prices[0]);
    		return fianl_profit;
    	}
    	int sep;
    
    	if (totaldays>2)
    	{
    		for (sep = 1; sep < totaldays - 1; sep++)
    		{
    			left_min = prices[0];
    			for (int li = 0; li <= sep; li++)
    			{
    				left_min = min(left_min, prices[li]);
    				l_max_profit = max(l_max_profit, prices[li] - left_min);
    			}
    			right_min = prices[sep];
    			for (int ri = sep; ri <= totaldays - 1; ri++)
    			{
    				right_min = min(right_min, prices[ri]);
    				r_max_profit = max(r_max_profit, prices[ri] - right_min);
    			}
    			max_profit = l_max_profit + r_max_profit;
    			l_max_profit = 0, r_max_profit = 0;
    			fianl_profit = max(fianl_profit, max_profit);
    		}
    		return fianl_profit;
    	}

Log in to reply
 

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