AFFILIATE LINKS
Great resource I use to learn algorithms.
40% off Tech Interview Pro: http://techinterviewpro.com/terriblewhiteboard
20% off CoderPro: http://coderpro.com/terriblewhiteboard
/**
* @param {number[]} prices
* @return {number}
*/
let maxProfit = function(prices) {
let profit = 0;
for (let i = 1; i < prices.length; i++) {
// only add to the profit when the price is going up
// the difference in price is the incremental profit
if (prices[i] > prices[i - 1]) {
profit += prices[i] - prices[i - 1];
}
}
return profit;
};