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.
Given n cuboids where the dimensions of the ith cuboid is cuboids[i] = [widthi, lengthi, heighti] (0-indexed). Choose a subset of cuboids and place them on each other.
You can place cuboid i on cuboid j if widthi <= widthj and lengthi <= lengthj and heighti <= heightj. You can rearrange any cuboid's dimensions by rotating it to put it on another cuboid.
Return the maximum height of the stacked cuboids.
Example 1:
Input: cuboids = [[50,45,20],[95,37,53],[45,23,12]] Output: 190 Explanation: Cuboid 1 is placed on the bottom with the 53x37 side facing down with height 95. Cuboid 0 is placed next with the 45x20 side facing down with height 50. Cuboid 2 is placed next with the 23x12 side facing down with height 45. The total height is 95 + 50 + 45 = 190.
Example 2:
Input: cuboids = [[38,25,45],[76,35,3]] Output: 76 Explanation: You can't place any of the cuboids on the other. We choose cuboid 1 and rotate it so that the 35x3 side is facing down and its height is 76.
Example 3:
Input: cuboids = [[7,11,17],[7,17,11],[11,7,17],[11,17,7],[17,7,11],[17,11,7]] Output: 102 Explanation: After rearranging the cuboids, you can see that all cuboids have the same dimension. You can place the 11x7 side down on all cuboids so their heights are 17. The maximum height of stacked cuboids is 6 * 17 = 102.
Constraints:
n == cuboids.length1 <= n <= 1001 <= widthi, lengthi, heighti <= 100Problem summary: Given n cuboids where the dimensions of the ith cuboid is cuboids[i] = [widthi, lengthi, heighti] (0-indexed). Choose a subset of cuboids and place them on each other. You can place cuboid i on cuboid j if widthi <= widthj and lengthi <= lengthj and heighti <= heightj. You can rearrange any cuboid's dimensions by rotating it to put it on another cuboid. Return the maximum height of the stacked cuboids.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
[[50,45,20],[95,37,53],[45,23,12]]
[[38,25,45],[76,35,3]]
[[7,11,17],[7,17,11],[11,7,17],[11,17,7],[17,7,11],[17,11,7]]
the-number-of-weak-characters-in-the-game)maximum-number-of-groups-entering-a-competition)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1691: Maximum Height by Stacking Cuboids
class Solution {
public int maxHeight(int[][] cuboids) {
for (var c : cuboids) {
Arrays.sort(c);
}
Arrays.sort(cuboids,
(a, b) -> a[0] == b[0] ? (a[1] == b[1] ? a[2] - b[2] : a[1] - b[1]) : a[0] - b[0]);
int n = cuboids.length;
int[] f = new int[n];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < i; ++j) {
if (cuboids[j][1] <= cuboids[i][1] && cuboids[j][2] <= cuboids[i][2]) {
f[i] = Math.max(f[i], f[j]);
}
}
f[i] += cuboids[i][2];
}
return Arrays.stream(f).max().getAsInt();
}
}
// Accepted solution for LeetCode #1691: Maximum Height by Stacking Cuboids
func maxHeight(cuboids [][]int) int {
for _, c := range cuboids {
sort.Ints(c)
}
sort.Slice(cuboids, func(i, j int) bool {
a, b := cuboids[i], cuboids[j]
return a[0] < b[0] || a[0] == b[0] && (a[1] < b[1] || a[1] == b[1] && a[2] < b[2])
})
n := len(cuboids)
f := make([]int, n)
for i := range f {
for j := 0; j < i; j++ {
if cuboids[j][1] <= cuboids[i][1] && cuboids[j][2] <= cuboids[i][2] {
f[i] = max(f[i], f[j])
}
}
f[i] += cuboids[i][2]
}
return slices.Max(f)
}
# Accepted solution for LeetCode #1691: Maximum Height by Stacking Cuboids
class Solution:
def maxHeight(self, cuboids: List[List[int]]) -> int:
for c in cuboids:
c.sort()
cuboids.sort()
n = len(cuboids)
f = [0] * n
for i in range(n):
for j in range(i):
if cuboids[j][1] <= cuboids[i][1] and cuboids[j][2] <= cuboids[i][2]:
f[i] = max(f[i], f[j])
f[i] += cuboids[i][2]
return max(f)
// Accepted solution for LeetCode #1691: Maximum Height by Stacking Cuboids
struct Solution;
impl Solution {
fn max_height(mut cuboids: Vec<Vec<i32>>) -> i32 {
let n = cuboids.len();
for i in 0..n {
cuboids[i].sort_unstable();
}
cuboids.sort_unstable();
let mut dp: Vec<i32> = vec![0; n];
for i in 0..n {
dp[i] = cuboids[i][2];
'outer: for j in 0..i {
for k in 0..3 {
if cuboids[j][k] > cuboids[i][k] {
continue 'outer;
}
}
dp[i] = dp[i].max(dp[j] + cuboids[i][2]);
}
}
dp.into_iter().max().unwrap()
}
}
#[test]
fn test() {
let cuboids = vec_vec_i32![[50, 45, 20], [95, 37, 53], [45, 23, 12]];
let res = 190;
assert_eq!(Solution::max_height(cuboids), res);
let cuboids = vec_vec_i32![[38, 25, 45], [76, 35, 3]];
let res = 76;
assert_eq!(Solution::max_height(cuboids), res);
let cuboids = vec_vec_i32![
[7, 11, 17],
[7, 17, 11],
[11, 7, 17],
[11, 17, 7],
[17, 7, 11],
[17, 11, 7]
];
let res = 102;
assert_eq!(Solution::max_height(cuboids), res);
}
// Accepted solution for LeetCode #1691: Maximum Height by Stacking Cuboids
function maxHeight(cuboids: number[][]): number {
for (const c of cuboids) {
c.sort((a, b) => a - b);
}
cuboids.sort((a, b) => {
if (a[0] !== b[0]) {
return a[0] - b[0];
}
if (a[1] !== b[1]) {
return a[1] - b[1];
}
return a[2] - b[2];
});
const n = cuboids.length;
const f = Array(n).fill(0);
for (let i = 0; i < n; ++i) {
for (let j = 0; j < i; ++j) {
const ok = cuboids[j][1] <= cuboids[i][1] && cuboids[j][2] <= cuboids[i][2];
if (ok) f[i] = Math.max(f[i], f[j]);
}
f[i] += cuboids[i][2];
}
return Math.max(...f);
}
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.