Created
October 30, 2015 04:56
-
-
Save fluffywaffles/6c264bb0b407c8a27621 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Array.sort(byVolume) | |
function byVolume (a, b) { | |
va = a[0] + a[1] + a[2] | |
vb = b[0] + b[1] + b[2] | |
if (va > vb) | |
return -1 | |
else if (vb > va) | |
return 1 | |
return 0 | |
} | |
// Answer the question | |
function maxNestedBoxes (boxes) { | |
var size = boxes.length | |
, i = size - 1 | |
, memo = [] | |
memo[size] = 0 | |
function nextFeasible (i) { | |
var box = boxes[i] | |
for (; i < size; i++) { | |
if (box[0] > boxes[i][0] | |
&& box[1] > boxes[i][1] | |
&& box[2] > boxes[i][2]) { | |
return i | |
} | |
} | |
return i | |
} | |
for(; i >= 0; i--) { | |
memo[i] = Math.max(1 + memo[nextFeasible(i)], | |
memo[i + 1]) | |
} | |
return memo | |
} | |
var test = [ [ 6, 6, 6 ], [ 1, 2, 3 ], [ 1, 1, 1 ] ] | |
test.sort(byVolume) | |
console.log(maxNestedBoxes(test)) | |
var test2 = [ [ 3, 4, 1 ], [ 2, 2, 2 ], [ 2, 1, 1 ], [ 3, 3, 3 ] ] | |
test2.sort(byVolume) | |
console.log(maxNestedBoxes(test2)) | |
var test3 = [ [ 7, 7, 7 ], [ 1, 1, 1 ], [ 1, 2, 3 ], [ 2, 3, 4 ], [ 3, 4, 5 ] ] | |
test3.sort(byVolume) | |
console.log(maxNestedBoxes(test3)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment