forked from TheAlgorithms/JavaScript
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRodCutting.js
More file actions
Latest commit
17 lines (14 loc) · 482 Bytes
/
Copy pathRodCutting.js
File metadata and controls
17 lines (14 loc) · 482 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
* 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.
*/
exportfunctionrodCut(prices,n){
constmemo=newArray(n+1)
memo[0]=0
for(leti=1;i<=n;i++){
letmaxVal=Number.MIN_VALUE
for(letj=0;j<i;j++){maxVal=Math.max(maxVal,prices[j]+memo[i-j-1])}
memo[i]=maxVal
}
returnmemo[n]
}