Skip to content
Prev Previous commit
Add input validation to rod cutting algorithm
  • Loading branch information
MayankSharma-2812 committed Feb 1, 2026
commit 569e554db8c996a1635f803f07a312e714786152
12 changes: 12 additions & 0 deletions Dynamic-Programming/RodCutting.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@
*/

export function rodCut(prices, n) {
if (!Array.isArray(prices)) {
throw new TypeError('Prices must be an array')
}

if (!Number.isInteger(n) || n < 0) {
throw new TypeError('Rod length must be a non-negative integer')
}

if (n === 0) {
return 0
}

const memo = new Array(n + 1)
memo[0] = 0

Expand Down