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
Selling Pieces of Wood - DP Solution INPUT m=3 × n=5 Price Catalog: h w price 1 4 $2 2 2 $7 2 1 $3 m = 3, n = 5 prices = [ [1,4,2],[2,2,7],[2,1,3] ] ALGORITHM STEPS 1 Initialize DP Table dp[i][j] = max value for piece of size i × j 2 Fill Base Prices Set dp[h][w] = price for each priced dimension 3 Try All Cuts Horizontal: dp[k][j]+dp[i-k][j] Vertical: dp[i][k]+dp[i][j-k] 4 Take Maximum dp[i][j] = max(no cut, h-cut, v-cut) Optimal Cutting: 2×2 2×2 2×1 1×4 + extra 2×(2×2) + (2×1) + (1×4) = 7+7+3+2 = 19 FINAL RESULT 1×4=$2 2×2=$7 2×1=$3 2×2=$7 Calculation: 2×(2×2) = $14 1×(2×1) = $3 1×(1×4) = $2 OUTPUT $19 OK - Maximum profit achieved! dp[3][5] = 19 Key Insight: This is a 2D cutting stock problem solved with dynamic programming. For each subproblem dp[i][j], we consider: (1) selling as-is if priced, (2) all horizontal cuts, (3) all vertical cuts. Time: O(m×n×(m+n)) | Space: O(m×n) | The optimal substructure allows bottom-up computation. TutorialsPoint - Selling Pieces of Wood | Optimal DP Solution
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