Skip to content

Create RodCutting.js #776

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

Merged
Merged
Changes from 1 commit
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
Next Next commit
Create RodCutting.js
  • Loading branch information
raghhavtaneja authored Oct 11, 2021
commit 0312ee731ebbd4236eafcd33fbd83444643df4fb
27 changes: 27 additions & 0 deletions Dynamic-Programming/RodCutting.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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.
*/

function rodCut(prices, n){
let memo = new Array(n + 1);
memo[0] = 0;

for (let i = 1; i<=n; i++)
{
let max_val = Number.MIN_VALUE;
for (let j = 0; j < i; j++)
max_val = Math.max(max_val, prices[j] + memo[i - j - 1]);
memo[i] = max_val;
}

return memo[n];
}

function main(){
let arr = [1, 5, 4, 2, 1, 11, 19, 12];
let n = arr.length;
console.log('Maximum Possible Profit is : '+ rodCut(arr,n));
}

main();