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
Original Wood 4ร—3Worth: ? dollarsHorizontal Cut2ร—3Try cuts orsell partsBest: $852ร—3Cache hit!Return $85instantlyMemoization Table(1,1) โ†’ $15 โœ“(1,2) โ†’ $30 โœ“(2,1) โ†’ $25 โœ“(2,3) โ†’ $85 โœ“(4,3) โ†’ calculating...(3,2) โ†’ pendingSame problem, instant answer!Optimization Benefitsโšก Each subproblem solved once๐Ÿง  Remember solutions๐Ÿ“ˆ Polynomial vs exponential๐Ÿ’ฐ Always find optimal profit๐ŸŽฏ Key StrategyBreak down large problems into smaller ones,remember solutions, and build optimal answerfrom the bottom up!
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.
Asked in
Google 42 Amazon 38 Meta 29 Microsoft 24
78.0K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen