Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add new algorithm rodcutting2 in dp #1603

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

Draft
Sudip200 wants to merge 2 commits into TheAlgorithms:master
base: master
Choose a base branch
Loading
from Sudip200:RodCutting2
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions Dynamic-Programming/RodCutting2.js
View file Open in desktop
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
View file Open in desktop
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
}

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