This content has been marked as final. Show 3 replies
Sounds like you might want a simple greedy algorithm. Sort by filesize, then start adding the largest files; if one would put it over the limit, skip it and try the next.
Of course, it may not always be optimal - but it'll be a heck of a lot easier to implement and take a heck of a lot less time to run.
If you want an optimal solution, you need to use dynamic programming. If you want to do it, read through [this short pdf|http://web.archive.org/web/20060906091630/www.csl.mtu.edu/cs4321/www/Lectures/knapsack2.pdf] .
Hey, thanks a lot! The 0-1 Knapsack algorithm seems to be exactly what I was looking for!
Perhaps a bit late, but you could also try to search for "bin packing problem" for more information on the matter.