Just like with any dynamic programming problem, we need to produce a matrix
very nice code, I found it much clearer than the blog post
The key intuition is that by considering the items sequentially, you only care about being able to reach a given sum rather than caring about all the subsets that can reach that sum. So the performance is linear in the number of different sums rather than the number of subsets.
The spec for the problem he was actually trying to solve said that you needed to provide the set of activities in sorted order. The list is generated in no particular order, so I sorted them before returning them. (A Python array sort sorts the array in place.)
We get a list of up to 50 activities and an associated calorie value for each (either positive or negative), we need to find a subset of activities where the sum of all the calorie values is zero.
---
Did anyone else here immediately think to write a function hard coded to return the empty set?
Hehe yes, and I feel kinda bad for that. I think it's some permanent damage that I picked up from pair programming using unit tests and my desire to troll my pair partner into doing all the actual typing.
I did a very similar investigation 5 years ago, to find the optimal way to fill-up a DVD from a set of files of various sizes (more than a DVD's worth). Have a look: