Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
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 <= 2001 <= prices.length <= 2 * 104prices[i].length == 31 <= hi <= m1 <= wi <= n1 <= pricei <= 106(hi, wi) are pairwise distinct.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
3 5 [[1,4,2],[2,2,7],[2,1,3]]
4 6 [[3,2,10],[1,4,2],[4,1,3]]
tiling-a-rectangle-with-the-fewest-squares)number-of-ways-of-cutting-a-pizza)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2312: Selling Pieces of Wood
class Solution {
private int[][] d;
private Long[][] f;
public long sellingWood(int m, int n, int[][] prices) {
d = new int[m + 1][n + 1];
f = new Long[m + 1][n + 1];
for (var p : prices) {
d[p[0]][p[1]] = p[2];
}
return dfs(m, n);
}
private long dfs(int h, int w) {
if (f[h][w] != null) {
return f[h][w];
}
long ans = d[h][w];
for (int i = 1; i < h / 2 + 1; ++i) {
ans = Math.max(ans, dfs(i, w) + dfs(h - i, w));
}
for (int i = 1; i < w / 2 + 1; ++i) {
ans = Math.max(ans, dfs(h, i) + dfs(h, w - i));
}
return f[h][w] = ans;
}
}
// Accepted solution for LeetCode #2312: Selling Pieces of Wood
func sellingWood(m int, n int, prices [][]int) int64 {
f := make([][]int64, m+1)
d := make([][]int, m+1)
for i := range f {
f[i] = make([]int64, n+1)
for j := range f[i] {
f[i][j] = -1
}
d[i] = make([]int, n+1)
}
for _, p := range prices {
d[p[0]][p[1]] = p[2]
}
var dfs func(int, int) int64
dfs = func(h, w int) int64 {
if f[h][w] != -1 {
return f[h][w]
}
ans := int64(d[h][w])
for i := 1; i < h/2+1; i++ {
ans = max(ans, dfs(i, w)+dfs(h-i, w))
}
for i := 1; i < w/2+1; i++ {
ans = max(ans, dfs(h, i)+dfs(h, w-i))
}
f[h][w] = ans
return ans
}
return dfs(m, n)
}
# Accepted solution for LeetCode #2312: Selling Pieces of Wood
class Solution:
def sellingWood(self, m: int, n: int, prices: List[List[int]]) -> int:
@cache
def dfs(h: int, w: int) -> int:
ans = d[h].get(w, 0)
for i in range(1, h // 2 + 1):
ans = max(ans, dfs(i, w) + dfs(h - i, w))
for i in range(1, w // 2 + 1):
ans = max(ans, dfs(h, i) + dfs(h, w - i))
return ans
d = defaultdict(dict)
for h, w, p in prices:
d[h][w] = p
return dfs(m, n)
// Accepted solution for LeetCode #2312: Selling Pieces of Wood
/**
* [2312] 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:
* <img alt="" src="https://assets.leetcode.com/uploads/2022/04/27/ex1.png" style="width: 239px; height: 150px;" />
* 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:
* <img alt="" src="https://assets.leetcode.com/uploads/2022/04/27/ex2new.png" style="width: 250px; height: 175px;" />
* 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 * 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.
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/selling-pieces-of-wood/
// discuss: https://leetcode.com/problems/selling-pieces-of-wood/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn selling_wood(m: i32, n: i32, prices: Vec<Vec<i32>>) -> i64 {
0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_2312_example_1() {
let m = 3;
let n = 5;
let prices = vec![vec![1, 4, 2], vec![2, 2, 7], vec![2, 1, 3]];
let result = 19;
assert_eq!(Solution::selling_wood(m, n, prices), result);
}
#[test]
#[ignore]
fn test_2312_example_2() {
let m = 4;
let n = 6;
let prices = vec![vec![3, 2, 10], vec![1, 4, 2], vec![4, 1, 3]];
let result = 32;
assert_eq!(Solution::selling_wood(m, n, prices), result);
}
}
// Accepted solution for LeetCode #2312: Selling Pieces of Wood
function sellingWood(m: number, n: number, prices: number[][]): number {
const f: number[][] = Array.from({ length: m + 1 }, () => Array(n + 1).fill(-1));
const d: number[][] = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));
for (const [h, w, p] of prices) {
d[h][w] = p;
}
const dfs = (h: number, w: number): number => {
if (f[h][w] !== -1) {
return f[h][w];
}
let ans = d[h][w];
for (let i = 1; i <= Math.floor(h / 2); i++) {
ans = Math.max(ans, dfs(i, w) + dfs(h - i, w));
}
for (let i = 1; i <= Math.floor(w / 2); i++) {
ans = Math.max(ans, dfs(h, i) + dfs(h, w - i));
}
return (f[h][w] = ans);
};
return dfs(m, n);
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.