Skip to content

Latest commit

 

History

History

2312

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

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:

Input: m = 3, n = 5, prices = [[1,4,2],[2,2,7],[2,1,3]]
Output: 19
Explanation: The diagram above shows a possible scenario. It consists of:
- 2 pieces of wood shaped 2 x 2, selling for a price of 2 * 7 = 14.
- 1 piece of wood shaped 2 x 1, selling for a price of 1 * 3 = 3.
- 1 piece of wood shaped 1 x 4, selling for a price of 1 * 2 = 2.
This obtains a total of 14 + 3 + 2 = 19 money earned.
It can be shown that 19 is the maximum amount of money that can be earned.

Example 2:

Input: m = 4, n = 6, prices = [[3,2,10],[1,4,2],[4,1,3]]
Output: 32
Explanation: The diagram above shows a possible scenario. It consists of:
- 3 pieces of wood shaped 3 x 2, selling for a price of 3 * 10 = 30.
- 1 piece of wood shaped 1 x 4, selling for a price of 1 * 2 = 2.
This obtains a total of 30 + 2 = 32 money earned.
It can be shown that 32 is the maximum amount of money that can be earned.
Notice that we cannot rotate the 1 x 4 piece of wood to obtain a 4 x 1 piece of wood.

 

Constraints:

  • 1 <= m, n <= 200
  • 1 <= prices.length <= 2 * 104
  • prices[i].length == 3
  • 1 <= hi <= m
  • 1 <= wi <= n
  • 1 <= pricei <= 106
  • All the shapes of wood (hi, wi) are pairwise distinct.

Companies: Palantir Technologies

Related Topics:
Array, Dynamic Programming, Memoization

Similar Questions:

Solution 1. Top-down DP

// OJ: https://leetcode.com/problems/selling-pieces-of-wood
// Author: github.com/lzl124631x
// Time: O(MN * (M + N))
// Space: O(MN)
class Solution {
public:
    long long sellingWood(int m, int n, vector<vector<int>>& P) {
        long long memo[201][201] = {}, p[201][201] = {};
        memset(memo, -1, sizeof(memo));
        for (auto &v : P) p[v[0]][v[1]] = v[2];
        function<long long(int, int)> dp = [&](int h, int w) -> long long {
            if (memo[h][w] != -1) return memo[h][w];
            memo[h][w] = p[h][w];
            for (int i = 1; i <= h / 2; ++i) {
                memo[h][w] = max(memo[h][w], dp(i, w) + dp(h - i, w));
            }
            for (int j = 1; j <= w / 2; ++j) {
                memo[h][w] = max(memo[h][w], dp(h, j) + dp(h, w - j));
            }
            return memo[h][w];
        };
        return dp(m, n);
    }
};

Solution 2. Bottom-up DP

// OJ: https://leetcode.com/problems/selling-pieces-of-wood
// Author: github.com/lzl124631x
// Time: O(MN * (M + N))
// Space: O(MN)
class Solution {
public:
    long long sellingWood(int m, int n, vector<vector<int>>& P) {
        long long dp[201][201] = {};
        for (auto &p : P) dp[p[0]][p[1]] = p[2];
        for (int h = 1; h <= m; ++h) {
            for (int w = 1; w <= n; ++w) {
                for (int x = 1; x <= h / 2; ++x) dp[h][w] = max(dp[h][w], dp[x][w] + dp[h - x][w]);
                for (int x = 1; x <= w / 2; ++x) dp[h][w] = max(dp[h][w], dp[h][x] + dp[h][w - x]);
            }
        }
        return dp[m][n];
    }
};