2021-10-12 16:55:56 +05:30
|
|
|
/*
|
2021-10-11 21:54:41 +05:30
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2021-10-12 16:55:56 +05:30
|
|
|
export function rodCut (prices, n) {
|
|
|
|
|
const memo = new Array(n + 1)
|
|
|
|
|
memo[0] = 0
|
2021-10-12 11:06:21 +05:30
|
|
|
|
2021-10-12 16:55:56 +05:30
|
|
|
for (let i = 1; i <= n; i++) {
|
|
|
|
|
let maxVal = Number.MIN_VALUE
|
|
|
|
|
for (let j = 0; j < i; j++) { maxVal = Math.max(maxVal, prices[j] + memo[i - j - 1]) }
|
|
|
|
|
memo[i] = maxVal
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return memo[n]
|
|
|
|
|
}
|