Problem Description
Alice and Bob compete in an archery contest where the target is divided into scoring sections from 0 to 11. Alice has already shot her arrows with a known distribution across these sections. Bob now has numArrows arrows and wants to maximize his total score. To win a section and obtain its k points, Bob must shoot strictly more arrows in that section than Alice did (i.e. at least aliceArrows[k] + 1). If he does not exceed Alice’s count for a section, then Alice gets those points (unless neither shot any arrows at that section). The goal is to find one valid allocation of Bob’s arrows across the sections such that the sum of arrows equals numArrows and his total point score is maximized.
Key Insights
- To win a section k, Bob must allocate (aliceArrows[k] + 1) arrows.
- Each section has a “cost” (number of arrows needed) and a “value” (the score k), turning the selection into a knapsack-like problem.
- With only 12 scoring sections, it is computationally feasible to iterate through all 2^12 possible subsets of sections.
- After selecting the best subset of sections to win, any leftover arrows can be assigned arbitrarily (for example, to section 0) since section 0’s score is 0 and extra arrows do not affect the win condition.
Space and Time Complexity
Time Complexity: O(2^12) (approximately 4096 iterations), which is efficient because the number of sections is fixed at 12. Space Complexity: O(12) for the additional storage for the solution and recursion/iteration variables.
Solution
The problem is approached by considering each section independently as an item in a knapsack. For each section k, Bob can win it by spending (aliceArrows[k] + 1) arrows to earn k points. We enumerate all possible combinations (subsets) of sections that Bob could win. For every subset, we sum up the cost (arrows needed) and the total points. If the cost does not exceed numArrows, we update our best solution if the current subset yields a higher total score. Once the optimal combination is identified, we convert this subset into an allocation array (bobArrows) where for every section that Bob wins, we assign (aliceArrows[k] + 1) arrows. Finally, we distribute any remaining arrows (numArrows minus allocated arrows) — for example, by adding them to section 0 (which gives 0 points) to ensure the sum equals numArrows.