122. Best Time to Buy and Sell Stock II
解题思路
- 由于可以在同一天交易,那么每次有差值即可做交易
- 遍历数组,如果后一天大于前一天即累加最大获利值 max
代码
/**
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function(prices) {
let max = 0
for(let i = 0; i < prices.length; i++) {
if(prices[i] < prices[i + 1]) {
let profit = prices[i + 1] - prices[i]
max += profit
}
}
return max
};