LeetCode --- 贪心算法 --- 122. 买卖股票的最佳时机 II

贪心算法最粗暴的理解就是:眼观局限,只能选择当下看起来最好的

这个题目在官方的答案中有三种解决方案:暴力法;峰谷法;简单的一次遍历

我采用的就是最后一种

Profit Graph

public class Solution {
    public int MaxProfit(int[] prices) {
         var maxProfit = 0;
            for (int i = 1; i < prices.Length; i++)
            {
                if (prices[i] - prices[i - 1] > 0)
                {
                    maxProfit += prices[i] - prices[i - 1];
                }
            }

            return maxProfit;
    }
}
发布了71 篇原创文章 · 获赞 89 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/u012371712/article/details/104190551