[LeetCode] Maximum Points in an Archery Competition

2212. Maximum Points in an Archery Competition

Alice and Bob are opponents in an archery competition. The competition has set the following rules:

  1. Alice first shoots numArrows arrows and then Bob shoots numArrows arrows.
  2. The points are then calculated as follows:
  1. The target has integer scoring sections ranging from 0 to 11 inclusive.
  2. For each section of the target with score 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.
  3. However, if 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Solution {
vector<int> res;
int score = -1;

void helper(int a, int s, vector<int>& alice, vector<int>& bob) {
if(a < 0) return;
if(s == 12) {
int b = 0;
for(int i = 0; i < 12; i++) {
if(bob[i] > alice[i])
b += i;
}
if(score < b) {
score = b;
res = bob;
if(a)
res[0] += a;
}
} else {
helper(a, s + 1, alice, bob);
bob[s] = alice[s] + 1;
helper(a - bob[s], s + 1, alice, bob);
bob[s] = 0;
}
}
public:
vector<int> maximumBobPoints(int numArrows, vector<int>& aliceArrows) {
res = vector<int>(12,0);
vector<int> bob(12,0);
helper(numArrows, 0, aliceArrows, bob);

return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/03/20/PS/LeetCode/maximum-points-in-an-archery-competition/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.