TopCoder problem solution: ChristmasBatteries
This post provides a solution to the TopCoder problem of ChristmasBatteries . This problem can be modeled as a 0-1 Knapsack problem . We can mode each toy as an item that can be put into the knapsack. The total weight allowed in the knapsack is the total number of available batteries. Each toy has two properties: fun and required batteries. The "fun" parameter is the value of the item. The required batteries is the weight of the item. The ChristmasBatteries problem can be solved by finding a subset of the toys that will maximize the fun while subjected to the constraint of the total number of available batteries. Note that the number of batteries is an integer, this is crucial for the Dynamic Programming solution to work. Implementation of the 0-1 Knapsack problem using Dynamic Programming First, we define an interface that represents a single abstract item that can be stored in a knapsack: public interface KnapsackItem { double getValue (); int getWeight (); } Then...