If you're not taking rotation into account, then I think this would be quite easily solvable using an Octree. Using the tree, you can detect the edges of the polyhedron(s) formed by existing cubes, and then calculate new boxes to wrap it.