Skip to content

Commit 569e554

Browse files
Add input validation to rod cutting algorithm
1 parent 142c98c commit 569e554

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

Dynamic-Programming/RodCutting.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@
44
*/
55

66
export function rodCut(prices, n) {
7+
if (!Array.isArray(prices)) {
8+
throw new TypeError('Prices must be an array')
9+
}
10+
11+
if (!Number.isInteger(n) || n < 0) {
12+
throw new TypeError('Rod length must be a non-negative integer')
13+
}
14+
15+
if (n === 0) {
16+
return 0
17+
}
18+
719
const memo = new Array(n + 1)
820
memo[0] = 0
921

0 commit comments

Comments
 (0)