From 91b83790bb0b429a3f95aa6480f2fd5c7b2c600b Mon Sep 17 00:00:00 2001 From: Sudipto Das Date: 2024年1月30日 10:05:01 +0530 Subject: [PATCH 1/2] add new algorithm rodcutting2 --- Dynamic-Programming/RodCutting2.js | 31 ++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Dynamic-Programming/RodCutting2.js diff --git a/Dynamic-Programming/RodCutting2.js b/Dynamic-Programming/RodCutting2.js new file mode 100644 index 0000000000..f87c3d5c7f --- /dev/null +++ b/Dynamic-Programming/RodCutting2.js @@ -0,0 +1,31 @@ +/* + * You are given a rod of 'n' length and an array of prices associated with all the lengths less than 'n'. + * Find the maximum profit possible by cutting the rod and selling the pieces. + * Read more about the problem here: https://kallalakshminarayana888.medium.com/rod-cutting-problem-using-top-down-dynamic-programming-me-ac4655139199 + */ + +function memoizedCutRod(prices, n) { + let memo = new Array(n + 1).fill(-1) + return memoizedCutRodAux(prices, n, memo) + } + + function memoizedCutRodAux(prices, n, memo) { + if (memo[n]>= 0) { + return memo[n] + } + let maxVal = Number.MIN_VALUE + if (n == 0) { + maxVal = 0 + } else { + for (let i = 0; i < n; i++) { + maxVal = Math.max( + maxVal, + prices[i] + memoizedCutRodAux(prices, n - i - 1, memo) + ) + } + } + memo[n] = maxVal + return maxVal + } + + export { memoizedCutRod } \ No newline at end of file From 034acb1bdd8e793b361e3c48c4023ae74811acec Mon Sep 17 00:00:00 2001 From: Sudipto Das Date: 2024年1月30日 10:07:20 +0530 Subject: [PATCH 2/2] add all files --- Dynamic-Programming/RodCutting2.js | 42 +++++++++++++++--------------- Maths/MobiusFunction.js | 4 +-- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/Dynamic-Programming/RodCutting2.js b/Dynamic-Programming/RodCutting2.js index f87c3d5c7f..f42f0b0ea1 100644 --- a/Dynamic-Programming/RodCutting2.js +++ b/Dynamic-Programming/RodCutting2.js @@ -5,27 +5,27 @@ */ function memoizedCutRod(prices, n) { - let memo = new Array(n + 1).fill(-1) - return memoizedCutRodAux(prices, n, memo) + let memo = new Array(n + 1).fill(-1) + return memoizedCutRodAux(prices, n, memo) +} + +function memoizedCutRodAux(prices, n, memo) { + if (memo[n]>= 0) { + return memo[n] } - - function memoizedCutRodAux(prices, n, memo) { - if (memo[n]>= 0) { - return memo[n] - } - let maxVal = Number.MIN_VALUE - if (n == 0) { - maxVal = 0 - } else { - for (let i = 0; i < n; i++) { - maxVal = Math.max( - maxVal, - prices[i] + memoizedCutRodAux(prices, n - i - 1, memo) - ) - } + let maxVal = Number.MIN_VALUE + if (n == 0) { + maxVal = 0 + } else { + for (let i = 0; i < n; i++) { + maxVal = Math.max( + maxVal, + prices[i] + memoizedCutRodAux(prices, n - i - 1, memo) + ) } - memo[n] = maxVal - return maxVal } - - export { memoizedCutRod } \ No newline at end of file + memo[n] = maxVal + return maxVal +} + +export { memoizedCutRod } diff --git a/Maths/MobiusFunction.js b/Maths/MobiusFunction.js index bd268b8bbd..4239d6ab31 100644 --- a/Maths/MobiusFunction.js +++ b/Maths/MobiusFunction.js @@ -28,6 +28,6 @@ export const mobiusFunction = (number) => { return primeFactorsArray.length !== new Set(primeFactorsArray).size ? 0 : primeFactorsArray.length % 2 === 0 - ? 1 - : -1 + ? 1 + : -1 }

AltStyle によって変換されたページ (->オリジナル) /