From 8afe254530dd356e10ffe55dd028685512f8ac80 Mon Sep 17 00:00:00 2001 From: MahaleHarsh <96824988+MahaleHarsh@users.noreply.github.com> Date: Wed, 6 Nov 2024 11:02:44 +0530 Subject: [PATCH 1/2] Update leastCommonMultiple.js applied the reduce method to iteratively compute the LCM across all numbers in the array, combining each pair of results. Additionally, I used the Euclidean algorithm for GCD calculation, which optimizes the LCM computation by reducing the multiplication size --- .../math/least-common-multiple/leastCommonMultiple.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/algorithms/math/least-common-multiple/leastCommonMultiple.js b/src/algorithms/math/least-common-multiple/leastCommonMultiple.js index 26a7681039..d8b8fe77eb 100644 --- a/src/algorithms/math/least-common-multiple/leastCommonMultiple.js +++ b/src/algorithms/math/least-common-multiple/leastCommonMultiple.js @@ -1,11 +1,12 @@ import euclideanAlgorithm from '../euclidean-algorithm/euclideanAlgorithm'; /** - * @param {number} a - * @param {number} b - * @return {number} + * Finds the LCM of an array of numbers. + * @param {number[]} arr - Array of numbers + * @return {number} - LCM of the entire array */ +export default function leastCommonMultipleArray(arr) { + if (arr.length === 0) return 0; -export default function leastCommonMultiple(a, b) { - return ((a === 0) || (b === 0)) ? 0 : Math.abs(a * b) / euclideanAlgorithm(a, b); + return arr.reduce((lcm, num) => leastCommonMultiple(lcm, num), 1); } From 80fa712074b7d677124003d7016f8f200ce2a2f7 Mon Sep 17 00:00:00 2001 From: MahaleHarsh <96824988+MahaleHarsh@users.noreply.github.com> Date: Wed, 6 Nov 2024 11:26:52 +0530 Subject: [PATCH 2/2] Update accumulatorBestTimeToBuySellStocks.js removed the Math.max calculation and directly added profit only on days when the stock price increased, simplifying the logic and improving readability. This greedy approach accumulates profit efficiently by capturing only the upward price trends. --- .../accumulatorBestTimeToBuySellStocks.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/algorithms/uncategorized/best-time-to-buy-sell-stocks/accumulatorBestTimeToBuySellStocks.js b/src/algorithms/uncategorized/best-time-to-buy-sell-stocks/accumulatorBestTimeToBuySellStocks.js index 9e244c89e1..b931757c1e 100644 --- a/src/algorithms/uncategorized/best-time-to-buy-sell-stocks/accumulatorBestTimeToBuySellStocks.js +++ b/src/algorithms/uncategorized/best-time-to-buy-sell-stocks/accumulatorBestTimeToBuySellStocks.js @@ -1,20 +1,21 @@ /** * Finds the maximum profit from selling and buying the stocks. - * ACCUMULATOR APPROACH. + * Greedy approach to accumulate profit only on price increases. * * @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(); +const bestTimeToBuySellStocks = (prices, visit = () => {}) => { let profit = 0; - for (let day = 1; day < prices.length; day += 1) { + for (let day = 1; day < prices.length; day++) { 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); + // Add only if today's price is higher than yesterday's + if (prices[day] > prices[day - 1]) { + profit += prices[day] - prices[day - 1]; + } } return profit; }; -export default accumulatorBestTimeToBuySellStocks; +export default bestTimeToBuySellStocks;