Selling Pieces of Wood

You are given two integers m and n that represent the height and width of a rectangular piece of wood. You are also given a 2D integer array prices, where prices[i] = [hi, wi, pricei] indicates you can sell a rectangular piece of wood of height hi and width wi for pricei dollars.

To cut a piece of wood, you must make a vertical or horizontal cut across the entire height or width of the piece to split it into two smaller pieces. After cutting a piece of wood into some number of smaller pieces, you can sell pieces according to prices. You may sell multiple pieces of the same shape, and you do not have to sell all the shapes. The grain of the wood makes a difference, so you cannot rotate a piece to swap its height and width.

Return the maximum money you can earn after cutting an m x n piece of wood.

Note that you can cut the piece of wood as many times as you want.

Example 1
Inputm = 3, n = 5, prices = [[1,4,2],[2,2,7],[2,1,3]]
Output19
The optimal cutting earns 14 from two 2 x 2 pieces, 3 from one 2 x 1 piece, and 2 from one 1 x 4 piece, for a total of 19.
Example 2
Inputm = 4, n = 6, prices = [[3,2,10],[1,4,2],[4,1,3]]
Output32
The optimal cutting earns 30 from three 3 x 2 pieces and 2 from one 1 x 4 piece, and the 1 x 4 piece cannot be rotated into 4 x 1.

Constraints

  • 1 <= m, n <= 200
  • 1 <= prices.length <= 2 * 10^4
  • prices[i].length == 3
  • 1 <= hi <= m
  • 1 <= wi <= n
  • 1 <= pricei <= 10^6
  • All the shapes of wood (hi, wi) are pairwise distinct.
</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate