Skip to content

Instantly share code, notes, and snippets.

@chrisc24
Created April 23, 2012 14:50
Show Gist options
  • Save chrisc24/2471392 to your computer and use it in GitHub Desktop.
Save chrisc24/2471392 to your computer and use it in GitHub Desktop.
BestBox V2
Create a data structure for each box
* Data about the box itself (x,y,z) (name?)
* bestDepth (initialize to 1)
* bestBox (initialize to null)
Sort Boxes ascending by size (x * y * z) (NLogN)
Starting with the smallest box:
Compare backwards with all smaller boxes.
Create a temporary list of smaller boxes that will fit inside it.
Pick one of the smaller boxes with the highest bestDepth.
This box is your bestBox. Your bestDepth is its bestDepth + 1.
(Those values are guaranteed to be filled in correctly because we are proceeding from smaller boxes).
If no boxes can fit inside leave bestDepth at 1 and bestBox at null
// You only calculate each box once in this method.
// If you revisit a box you only have to use the already calculated values. N calculations, each having to look at most N other boxes' values (which are already calculated).
// So This is O(N^2).
Find the box with the biggest bestDepth. This is your best box. To find the complete chain, iterate down through the bestBox value. (O(N) for each of these steps)
O(N^2) + 2(O(N)) = O(N^2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment