122. Best Time to Buy and Sell Stock II

来源:互联网 发布:光盘刻录软件nero 编辑:程序博客网 时间:2024/06/05 15:50

大概意思是不是只找最大差值,而是把所有的差值全部找出来

网上某个大佬提供了一个很巧妙的思路,就是把后一个减去前一个,然后得到差值如果大于零就加上去

int maxProfit(int* prices, int pricesSize) 
{
    int i=1,profit=0;
    for(;i<pricesSize;i++)
    {
        if(prices[i]-prices[i-1]>0)
            profit=profit+prices[i]-prices[i-1];
    }
    
    return profit;
}