Alice and Bob are opponents in an archery competition. The competition has set the following rules:
numArrows arrows and then Bob shoots numArrows arrows.0 to 11 inclusive.k (in between 0 to 11), say Alice and Bob have shot ak and bk arrows on that section respectively. If ak >= bk, then Alice takes k points. If ak < bk, then Bob takes k points.ak == bk == 0, then nobody takes k points.For example, if Alice and Bob both shot 2 arrows on the section with score 11, then Alice takes 11 points. On the other hand, if Alice shot 0 arrows on the section with score 11 and Bob shot 2 arrows on that same section, then Bob takes 11 points.
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 <= 105aliceArrows.length == bobArrows.length == 120 <= aliceArrows[i], bobArrows[i] <= numArrowssum(aliceArrows[i]) == numArrowsWhen you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To solve this archery competition puzzle using the brute force method, we need to explore every single possible way Alice could allocate her arrows across all the target sections. By checking every combination, we can figure out which choice yields the highest total score while keeping track of how many arrows she has left.
Here's how the algorithm would work step-by-step:
def maximumBobPoints(numArrows, aliceArrows):
maxScore = -1
bestAllocation = []
def explore(sectionIndex, arrowsLeft, currentScore, currentAllocation):
nonlocal maxScore, bestAllocation
if sectionIndex == 12:
if currentScore > maxScore:
maxScore = currentScore
bestAllocation = list(currentAllocation)
bestAllocation[0] += arrowsLeft
return
# We explore every valid arrow count to find the optimal point-scoring combination
requiredArrows = aliceArrows[sectionIndex] + 1
if arrowsLeft >= requiredArrows:
currentAllocation.append(requiredArrows)
explore(sectionIndex + 1, arrowsLeft - requiredArrows, currentScore + sectionIndex, currentAllocation)
currentAllocation.pop()
currentAllocation.append(0)
explore(sectionIndex + 1, arrowsLeft, currentScore, currentAllocation)
currentAllocation.pop()
explore(0, numArrows, 0, [])
return bestAllocationTo find the maximum possible score in the archery competition, we can use a clever search strategy that decides whether or not to target each scoring zone. By exploring the choices of either trying to win a zone or skipping it, we can find the combination that yields the highest total points while staying within our arrow limit.
Here's how the algorithm would work step-by-step:
class Solution:
def maximum_bob_points(self, total_num_arrows: int, alice_arrow_counts: list[int]) -> list[int]:
maximum_points_found = -1
best_arrow_distribution = []
def explore_scoring_zones(zone_index: int, arrows_remaining: int, current_points: int, current_distribution: list[int]):
nonlocal maximum_points_found, best_arrow_distribution
if zone_index < 0 or arrows_remaining == 0:
if arrows_remaining > 0:
current_distribution[0] += arrows_remaining
if current_points > maximum_points_found:
maximum_points_found = current_points
best_arrow_distribution = list(current_distribution)
if arrows_remaining > 0:
current_distribution[0] -= arrows_remaining
return
# We explore winning the current zone if we have enough arrows to beat Alice.
arrows_needed = alice_arrow_counts[zone_index] + 1
if arrows_remaining >= arrows_needed:
current_distribution[zone_index] = arrows_needed
explore_scoring_zones(zone_index - 1, arrows_remaining - arrows_needed, current_points + zone_index, current_distribution)
current_distribution[zone_index] = 0
# We also explore skipping the current zone to save arrows for higher-value targets.
explore_scoring_zones(zone_index - 1, arrows_remaining, current_points, current_distribution)
initial_distribution = [0] * 12
explore_scoring_zones(11, total_num_arrows, 0, initial_distribution)
return best_arrow_distribution| Case | How to Handle |
|---|---|
| Zero arrows available (numArrows = 0) | The algorithm will score 0 points by allocating 0 arrows to all sections and return the bobArrows array with all zeros. |
| Alice shoots 0 arrows in all sections | Bob can win every section by allocating just 1 arrow to section 0 and 0 arrows elsewhere, maximizing his score. |
| Alice shoots heavily in higher sections while Bob has few arrows | The DP or backtracking approach naturally prioritizes sections that yield the highest score-to-cost ratio for Bob. |
| Multiple sections require more arrows than Bob has total | The subset generation will only evaluate combinations whose total arrow count does not exceed numArrows. |
| Tie in points with multiple valid arrow distributions | Any valid combination achieving the maximum score is acceptable, but the algorithm can use a tie-breaker such as prioritizing lower sections. |
| Maximum number of sections (exactly 12 sections as defined by archery rules) | Since the number of sections is fixed at 12, a 2^12 subset enumeration runs in a fraction of a millisecond and scales effortlessly. |
| Leftover unspent arrows after maximizing score | Any remaining arrows can be safely dumped into section 0 since section 0 yields zero points and does not affect the optimal score. |
| Exact arrow budget match where every section requires arrows | The algorithm correctly evaluates whether winning a subset of sections fits strictly within the exact arrow budget without overflow. |