2312. Selling Pieces of Wood LeetCode Solution

In this guide, you will get 2312. Selling Pieces of Wood LeetCode Solution with the best time and space complexity. The solution to Selling Pieces of Wood problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Selling Pieces of Wood solution in C++
  4. Selling Pieces of Wood solution in Java
  5. Selling Pieces of Wood solution in Python
  6. Additional Resources
2312. Selling Pieces of Wood LeetCode Solution image

Problem Statement of 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.

See also  1754. Largest Merge Of Two Strings LeetCode Solution

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.

Complexity Analysis

  • Time Complexity: O(\max(m^2n, mn^2))
  • Space Complexity: O(mn)

2312. Selling Pieces of Wood LeetCode Solution in C++

class Solution {
 public:
  long long sellingWood(int m, int n, vector<vector<int>>& prices) {
    // dp[i][j] := the maximum money of cutting i x j piece of wood
    vector<vector<long>> dp(m + 1, vector<long>(n + 1));

    for (const vector<int>& p : prices) {
      const int h = p[0];
      const int w = p[1];
      const int price = p[2];
      dp[h][w] = price;
    }

    for (int i = 1; i <= m; ++i)
      for (int j = 1; j <= n; ++j) {
        for (int h = 1; h <= i / 2; ++h)
          dp[i][j] = max(dp[i][j], dp[h][j] + dp[i - h][j]);
        for (int w = 1; w <= j / 2; ++w)
          dp[i][j] = max(dp[i][j], dp[i][w] + dp[i][j - w]);
      }

    return dp[m][n];
  }
};
/* code provided by PROGIEZ */

2312. Selling Pieces of Wood LeetCode Solution in Java

class Solution {
  public long sellingWood(int m, int n, int[][] prices) {
    // dp[i][j] := the maximum money of cutting i x j piece of wood
    long[][] dp = new long[m + 1][n + 1];

    for (int[] p : prices) {
      final int h = p[0];
      final int w = p[1];
      final int price = p[2];
      dp[h][w] = price;
    }

    for (int i = 1; i <= m; ++i)
      for (int j = 1; j <= n; ++j) {
        for (int h = 1; h <= i / 2; ++h)
          dp[i][j] = Math.max(dp[i][j], dp[h][j] + dp[i - h][j]);
        for (int w = 1; w <= j / 2; ++w)
          dp[i][j] = Math.max(dp[i][j], dp[i][w] + dp[i][j - w]);
      }

    return dp[m][n];
  }
}
// code provided by PROGIEZ

2312. Selling Pieces of Wood LeetCode Solution in Python

class Solution:
  def sellingWood(self, m: int, n: int, prices: list[list[int]]) -> int:
    # dp[i][j] := the maximum money of cutting i x j piece of wood
    dp = [[0] * (n + 1) for _ in range(m + 1)]

    for h, w, price in prices:
      dp[h][w] = price

    for i in range(1, m + 1):
      for j in range(1, n + 1):
        for h in range(1, i // 2 + 1):
          dp[i][j] = max(dp[i][j], dp[h][j] + dp[i - h][j])
        for w in range(1, j // 2 + 1):
          dp[i][j] = max(dp[i][j], dp[i][w] + dp[i][j - w])

    return dp[m][n]
# code by PROGIEZ

Additional Resources

See also  868. Binary Gap LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.