Box stacking problem max height of stack possible (can be rotated). All the dimensions of the box beneath should be greater than the one stacked above it.
Javascript
const maxHeight = (cuboids) => { // Sort each cuboid dimensions. for (const cuboid of cuboids) { cuboid.sort((a, b) => a - b); } // Sort the cuboids from largest to smallest order. cuboids.sort((a, b) => { if (a[0] != b[0]) { return b[0] - a[0]; } if (a[1] != b[1]) { return b[1] - a[1]; } return b[2] - a[2]; }); // dp[i] means max height upto cuboid 'i' (row i). const dp = Array(cuboids.length); for (let i = 0; i < cuboids.length; i++) { // We choose the largest side of cuboid to be height to maximize it. dp[i] = cuboids[i][2]; for (let j = 0; j < i; j++) { if (cuboids[j][0] >= cuboids[i][0] && cuboids[j][1] >= cuboids[i][1] && cuboids[j][2] >= cuboids[i][2]) { dp[i] = Math.max(dp[i], dp[j] + cuboids[i][2]); } } } return Math.max(...dp); } console.log(maxHeight([ [1, 1, 1 ], [2, 3, 10], [2, 4, 1 ], ])); // 15