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