Maximum Points in an Archery Competition Problem
Description
LeetCode Problem 2212.
Alice and Bob are opponents in an archery competition. The competition has set the following rules:
- Alice first shoots
numArrowsarrows and then Bob shootsnumArrowsarrows. - The points are then calculated as follows:
- The target has integer scoring sections ranging from
0to11inclusive. - For each section of the target with score
k(in between0to11), say Alice and Bob have shota_kandb_karrows on that section respectively. Ifa_k >= b_k, then Alice takeskpoints. Ifa_k < b_k, then Bob takeskpoints. - However, if
a_k == b_k == 0, then nobody takeskpoints.
- The target has integer scoring sections ranging from
- For example, if Alice and Bob both shot
2arrows on the section with score11, then Alice takes11points. On the other hand, if Alice shot0arrows on the section with score11and Bob shot2arrows on that same section, then Bob takes11points.
You are given the integer numArrows and an integer array aliceArrows of size 12, which represents the number of arrows Alice shot on each scoring section from 0 to 11. Now, Bob wants to maximize the total number of points he can obtain.
Return the array bobArrows which represents the number of arrows Bob shot on each scoring section from 0 to 11. The sum of the values in bobArrows should equal numArrows.
If there are multiple ways for Bob to earn the maximum total points, return any one of them.
Example 1:

Input: numArrows = 9, aliceArrows = [1,1,0,1,0,0,2,1,0,1,2,0]
Output: [0,0,0,0,1,1,0,0,1,2,3,1]
Explanation: The table above shows how the competition is scored.
Bob earns a total point of 4 + 5 + 8 + 9 + 10 + 11 = 47.
It can be shown that Bob cannot obtain a score higher than 47 points.
Example 2:

Input: numArrows = 3, aliceArrows = [0,0,1,0,0,0,0,0,0,0,0,2]
Output: [0,0,0,0,0,0,0,0,1,1,1,0]
Explanation: The table above shows how the competition is scored.
Bob earns a total point of 8 + 9 + 10 = 27.
It can be shown that Bob cannot obtain a score higher than 27 points.
Constraints:
1 <= numArrows <= 10^5aliceArrows.length == bobArrows.length == 120 <= aliceArrows[i], bobArrows[i] <= numArrowssum(aliceArrows[i]) == numArrows
Solution
Approach: Enumerate Subsets of Sections
There are only 12 sections, so we can try every subset of sections that Bob wins. To win section i, Bob needs aliceArrows[i] + 1 arrows, and he then scores i points. Sections he does not win are better left empty, since arrows there are wasted.
For each non-empty subset, add up the arrows needed and the points earned. Among the subsets that fit within numArrows, keep the one with the highest score.
Then build the answer from the best subset: give each chosen section aliceArrows[i] + 1 arrows and put all leftover arrows in section 0, which costs Bob nothing.
Complexity: O(2m · m) time and O(1) extra space, where m = 12 is the number of sections.
class Solution:
def maximumBobPoints(self, numArrows: int, aliceArrows: List[int]) -> List[int]:
st = mx = 0
m = len(aliceArrows)
for mask in range(1, 1 << m):
cnt = s = 0
for i, x in enumerate(aliceArrows):
if mask >> i & 1:
s += i
cnt += x + 1
if cnt <= numArrows and s > mx:
mx = s
st = mask
ans = [0] * m
for i, x in enumerate(aliceArrows):
if st >> i & 1:
ans[i] = x + 1
numArrows -= ans[i]
ans[0] += numArrows
return ans