815. Tallest Crate Stack

A warehouse holds boxes listed in crates; each row has exactly three numbers, the side lengths of one box. Any box may be turned so that any of its three sides is the height.

You may pick some boxes and stack them in a tower. A box can sit on another only if, after both are turned however you like, each of its three side lengths is less than or equal to the matching side of the box below (the two footprint sides and the height are all compared). The height of the tower is the sum of the heights of the boxes in their chosen orientations.

Return the maximum tower height. Aim for time O(n^2) and extra space O(n).

Example 1

Input:
crates = [[4,6,3],[2,2,9],[5,5,1],[7,3,3]]
Output:
9
Explanation:

Standing the 2 x 2 x 9 crate upright gives height 9, and no other crate can join it in a taller tower.

Example 2

Input:
crates = [[8,2,2],[3,3,3]]
Output:
8
Explanation:

Neither crate fits inside the other in any rotation, so the best tower is the 8-high crate on its own.

Constraints

1 ≤ crates.length ≤ 800

crates[i].length == 3

1 ≤ crates[i][j] ≤ 100

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 1,200 msC++ 300 msJava 600 msJavaScript 600 msTypeScript 600 ms

Expected complexity

Time
O(n^2)
Space
O(n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…