Selling Pieces of Wood - Problem
Imagine you're a master woodworker who has just acquired a beautiful rectangular piece of wood with dimensions m × n. You have access to a price catalog that tells you exactly how much you can sell different rectangular pieces for.
Your goal is to maximize your profit by strategically cutting the wood and selling the pieces. You can make:
- Vertical cuts - slice through the entire height
- Horizontal cuts - slice through the entire width
The wood grain matters, so you cannot rotate pieces (height and width are fixed). You're given a 2D array prices where prices[i] = [hi, wi, pricei] means a piece of height hi and width wi sells for pricei dollars.
Key considerations:
- You can cut as many times as you want
- You can sell multiple pieces of the same dimensions
- You don't have to sell every piece you cut
- Some cuts might result in more valuable smaller pieces
Return the maximum money you can earn from your m × n piece of wood.
Input & Output
example_1.py — Basic Wood Cutting
$
Input:
m = 3, n = 5, prices = [[1,4,2],[2,2,7],[2,1,3]]
›
Output:
19
💡 Note:
The optimal strategy is to make a horizontal cut at position 2, creating two pieces: one 2×5 piece and one 1×5 piece. The 2×5 piece can be cut vertically into five 2×1 pieces (each worth 3), and the 1×5 piece can be cut vertically into one 1×4 piece (worth 2) and one 1×1 piece (unsold). Total: 5×3 + 2 + 0 = 17. Wait, let me recalculate: we can get two 2×2 pieces (7 each) and one 2×1 piece (3), plus one 1×4 piece (2) and one 1×1 piece. So 7+7+3+2 = 19.
example_2.py — No Cuts Needed
$
Input:
m = 4, n = 6, prices = [[3,2,10],[1,4,2],[4,1,3]]
›
Output:
32
💡 Note:
We can make vertical cuts to get six 4×1 pieces, each worth 3. Total profit = 6 × 3 = 18. Alternatively, we can make horizontal cuts and vertical cuts to get other combinations, but the optimal turns out to be different cuts yielding 32.
example_3.py — Edge Case - Single Cell
$
Input:
m = 1, n = 1, prices = [[1,1,5]]
›
Output:
5
💡 Note:
The wood is already 1×1 and matches exactly one price entry. We sell it directly for 5 dollars without any cuts.
Constraints
- 1 ≤ m, n ≤ 200
- 1 ≤ prices.length ≤ 2 × 104
- prices[i].length == 3
- 1 ≤ hi ≤ m
- 1 ≤ wi ≤ n
- 1 ≤ pricei ≤ 106
- Multiple entries may have the same dimensions - take the maximum price
Visualization
Tap to expand
Understanding the Visualization
1
Price Catalog Setup
Create a lookup table for all available piece prices, keeping the maximum price for duplicate dimensions
2
Recursive Exploration
For each piece, consider selling directly or making cuts. Try all possible horizontal and vertical cut positions
3
Memoization Magic
Store results for each piece size to avoid recalculating the same subproblems multiple times
4
Optimal Solution
Build up from smaller pieces to larger ones, always choosing the option that yields maximum profit
Key Takeaway
🎯 Key Insight: Memoization transforms an exponential brute force solution into an efficient polynomial-time algorithm by avoiding redundant calculations of the same subproblems.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code