Skip to content

Commit 0312ee7

Browse files
Create RodCutting.js
1 parent 5f601fa commit 0312ee7

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Dynamic-Programming/RodCutting.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* You are given a rod of 'n' length and an array of prices associated with all the lengths less than 'n'.
3+
* Find the maximum profit possible by cutting the rod and selling the pieces.
4+
*/
5+
6+
function rodCut(prices, n){
7+
let memo = new Array(n + 1);
8+
memo[0] = 0;
9+
10+
for (let i = 1; i<=n; i++)
11+
{
12+
let max_val = Number.MIN_VALUE;
13+
for (let j = 0; j < i; j++)
14+
max_val = Math.max(max_val, prices[j] + memo[i - j - 1]);
15+
memo[i] = max_val;
16+
}
17+
18+
return memo[n];
19+
}
20+
21+
function main(){
22+
let arr = [1, 5, 4, 2, 1, 11, 19, 12];
23+
let n = arr.length;
24+
console.log('Maximum Possible Profit is : '+ rodCut(arr,n));
25+
}
26+
27+
main();

0 commit comments

Comments
 (0)