Somehow, I got really invested in solving this:

Ignoring the fact that you can just order 7 fruit cups…

The way I’d solve it is to try to fit as many of the smallest number n into the total you want. If the remaining space isn’t filled by any of the other options, remove n from your running total and try to fill the remaining space as if it were a new container to fill (this should be easier/faster).

Eventually you may end up with no “smallest number n”s in your solution, but it’s the best place to start and you should be able to safely remove it from the algorithm on the next round of recursion. (This is a problem which would probably involve a lot of recursion in order to be efficient.) If/when that happens, or you’ve gotten down to the most “smallest number n”s you can hold and solve the problem, you treat the next largest number as your new n.

Theoretically this should work, although if it did, people probably wouldn’t be arguing about it.

…I got nerd sniped. (How many points are programmers?)

### Like this:

Like Loading...

*Related*