Test before you choose
transcript
Choose packs for a phone. Some packs clash. There is a size cap. Test before you choose.
Ask whether the pack clashes. Ask whether it fits the cap. If yes, take it and explore. Then undo it, and try the next.
Four packs. Sizes three, two, four, one. Packs zero and one clash. The cap is six. After the last pack, nine sets are valid. Sixteen sets existed, seven are cut.
With the test, the walk makes twenty calls. Without it, thirty-one. A cut removes a whole subtree. The gap grows with more packs.
Testing at the end walks every branch. Testing before the choice cuts early. The earlier, the bigger the saving. Put the test as high as you can.
Each branch returns a count. Add the counts up. Nothing needs to be stored. So large inputs are possible.
Always valid so far. Test, then choose. Undo, and go on. Full breakdown in the lecture.