Box Stacking
Problem Statement
You have n boxes. Each box has three sides: height, width and length. You can turn a box so that any one of its sides is the height. The other two sides then form its base.
Boxes can be stacked into a tower. A box can sit on another box only when both sides of its base are strictly smaller than the two sides of the base under it. Equal sides do not count. The same box may be used more than once in the tower, for example in two different turns.
Return the greatest height that a tower can reach.
Three lists of length n: height, width and length. Box i has sides height[i], width[i] and length[i].
The largest possible total height of a tower.
From bottom to top the heights are 10, 32, 4, 4, 6, 1, 3. They add up to 60. Each base is strictly smaller on both sides than the base below it, and some boxes are used twice in different turns.
From bottom to top the heights are 4, 6, 1, 1, 3. They add up to 15, and each base is strictly smaller than the one under it on both sides.
- • 1 ≤ n ≤ 100
- • 1 ≤ height[i], width[i], length[i] ≤ 10^6
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Recurse from the current top: each orientation whose base fits underneath is a choice; keep the tallest tower
dp[i] = height[i] + the best dp[j] over earlier orientations with a strictly bigger base; sort the 3N orientations first
Make the 3N orientations. Start from the floor. For the current top, try every orientation whose base is strictly bigger on both sides, add its height, and continue from that orientation. The answer is the largest total over all these choices.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like DP — Longest Increasing Subsequence translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.