Best Time to Buy and Sell Stock II

来源:互联网 发布:我国历年gdp数据 编辑:程序博客网 时间:2024/05/18 00:02

题目链接

思想:
贪心算法。只要第二天的价格比当天高,那么就买入然后第二天卖掉。

        int profit=0;        for(int i=0;i<prices.length-1;i++)        {            if(prices[i]<prices[i+1])            {                profit+=prices[i+1]-prices[i];            }        }        return profit;
0 0