一、题目地址
https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/
二、思路地址
https://www.bilibili.com/video/BV11a4y1W7N5
三、具体代码
/**
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function(prices) {
if(prices.length === 0) {
return 0;
}
// 贪心算法思路: 如果下一天的价格 大于 上一天的价格, 则总是使得利润变量profit递加上这两天价格之差
let profit = 0;//profit是利润变量
for(let i=0; i<prices.length-1; i++) {
if(prices[i] < prices[i+1]) {
profit += prices[i+1] - prices[i];
}
}
return profit;
};