Published by Dimitris Kougioumtzis · Updated
Maximize revenue by cutting a rod; price at index i applies to length i+1.
Try each first-piece length and combine its price with the best value for the remaining length.
When to use Rod cutting
Reusable piece pricing and unbounded optimization.
Choosing an alternative
A price-per-unit greedy rule lacks the needed general optimality argument.
Worked example: input and state changes
Example input
1,5,8,9 | 4
These checkpoints follow the actual example timeline. State details show the stored values and decisions; use the visualization to inspect every intervening step.
Step 0: Initial state.
State at this checkpoint
{ "values": [ 0, 0, 0, 0, 0 ], "active": [] }Step 3: Length 2: try a piece of length 2; best revenue 5.
State at this checkpoint
{ "values": [ 0, 1, 5, 0, 0 ], "active": [ 2 ] }Step 6: Length 3: try a piece of length 3; best revenue 8.
State at this checkpoint
{ "values": [ 0, 1, 5, 8, 0 ], "active": [ 3 ] }Step 8: Length 4: try a piece of length 2; best revenue 10.
State at this checkpoint
{ "values": [ 0, 1, 5, 8, 10 ], "active": [ 4 ] }Step 11: Maximum rod revenue = 10.
State at this checkpoint
{ "values": [ 0, 1, 5, 8, 10 ], "active": [ 4 ] }
Expected final state
Maximum rod revenue = 10.
Complexity and input bounds
Family: Dynamic programming. Time complexity: O(length × prices).
Space complexity: O(length) DP storage.
Working-space bounds describe algorithm state and any explicitly noted output, excluding the visualization's saved timeline. Recording frames can use more memory than running the function alone. Here n is sequence length, V and E are vertex and edge counts, and other parameters refer to the named input dimensions.
Prices (1–16 entries, 0–99) | rod length (0–40).
The playground validates these limits before starting. Downloaded functions are callable implementations, with parameter and output-buffer contracts in their comments; they do not all repeat the playground's input validation.
Common mistakes and limitations
The best decomposition can omit the apparently most attractive individual length.
Further reading
For background on algorithm analysis and the JavaScript function interface:
- MIT OpenCourseWare: algorithm design and analysis foundations
- MDN: JavaScript function parameters and return values
Editorial and testing approach · Browse all algorithm guides
Complete JavaScript implementation
1 2 3 4 5 6 7 8 9 10 | // a[i] is the nonnegative price of a piece of length i+1, n ≤ 16, length ≤ 40. Pieces may be repeated. Reference snippets assume validated playground inputs. Fixed scratch buffers have 256 slots. Sorting variants may use a different traversal strategy than the animation. function algorithm(a, n, length) { let dp = Array(256).fill(0); for (let size = 1; size <= length; size++) { for (let cut = 1; cut <= n && cut <= size; cut++) { let candidate = a[cut - 1] + dp[size - cut]; if (candidate > dp[size]) { dp[size] = candidate; } } } return dp[length]; } |
Source
Foundational dynamic-programming paper. It establishes the general method, not the first publication or exact recurrence of this particular teaching example.
- The theory of dynamic programming
Richard Bellman (1954). Foundational method paper.