diff --git a/Dynamic-Programming/RodCutting2.js b/Dynamic-Programming/RodCutting2.js new file mode 100644 index 0000000000..f42f0b0ea1 --- /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 } 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 によって変換されたページ (->オリジナル) /