Oleksii Trekhleb 2bbe12b83b
Add divide and conquer example: best time to buy and sell stocks (#612)
* Add divide and conquer example: best time to buy and sell stocks.

* Add divide and conquer example: best time to buy and sell stocks.

* Add dynamic programming version.
2020-12-23 17:24:49 +01:00

21 lines
710 B
JavaScript

/**
* Finds the maximum profit from selling and buying the stocks.
* ACCUMULATOR APPROACH.
*
* @param {number[]} prices - Array of stock prices, i.e. [7, 6, 4, 3, 1]
* @param {function(): void} visit - Visiting callback to calculate the number of iterations.
* @return {number} - The maximum profit
*/
const accumulatorBestTimeToBuySellStocks = (prices, visit = () => {}) => {
visit();
let profit = 0;
for (let day = 1; day < prices.length; day += 1) {
visit();
// Add the increase of the price from yesterday till today (if there was any) to the profit.
profit += Math.max(prices[day] - prices[day - 1], 0);
}
return profit;
};
export default accumulatorBestTimeToBuySellStocks;