Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new algorithm rodcutting2 in dp #1603

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 31 additions & 0 deletions Dynamic-Programming/RodCutting2.js
Original file line number Diff line number Diff line change
@@ -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 }
4 changes: 2 additions & 2 deletions Maths/MobiusFunction.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ export const mobiusFunction = (number) => {
return primeFactorsArray.length !== new Set(primeFactorsArray).size
? 0
: primeFactorsArray.length % 2 === 0
? 1
: -1
? 1
: -1
}