Problem Description
Given an m x n piece of wood and a list of available sellable pieces (each specified by height, width, and price), determine the maximum revenue obtainable by cutting the wood. You may cut the wood by making vertical or horizontal cuts across the entire piece, and you may sell any resulting piece if it exactly matches one of the provided dimensions (rotations are not allowed).
Key Insights
- Use dynamic programming to solve for maximum revenue for every sub-piece of wood.
- For every dimension (i x j), consider selling it directly if there is a price, or try every possible vertical and horizontal cut.
- The recurrence: dp[i][j] is the max between selling the piece directly (if available) and splitting it into two pieces, summing the best revenues obtainable from the split parts.
- Bottom-up DP is efficient since m and n are small (each up to 200), and we have to check cuts over all possible splits.
Space and Time Complexity
Time Complexity: O(m * n * (m + n))
Space Complexity: O(m * n)
Solution
We solve the problem with a bottom-up dynamic programming approach. We use a 2D dp array where dp[i][j] captures the maximum revenue obtainable from a wood piece of dimensions i x j. We initialize dp[i][j] with a direct sale price if the piece exactly matches one of the provided sizes; otherwise, it starts at zero. Then, for each dimension we iterate over all possible vertical and horizontal cuts to update dp[i][j] with the maximum revenue obtainable from any such split. This approach systematically builds up the answer from the smallest pieces to the full m x n piece.