贪心算法最粗暴的理解就是:眼观局限,只能选择当下看起来最好的
这个题目在官方的答案中有三种解决方案:暴力法;峰谷法;简单的一次遍历
我采用的就是最后一种
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;
}
}