2212. Maximum Points in an Archery Competition LeetCode Solution
In this guide, you will get 2212. Maximum Points in an Archery Competition LeetCode Solution with the best time and space complexity. The solution to Maximum Points in an Archery Competition problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Maximum Points in an Archery Competition solution in C++
- Maximum Points in an Archery Competition solution in Java
- Maximum Points in an Archery Competition solution in Python
- Additional Resources
Problem Statement of Maximum Points in an Archery Competition
Alice and Bob are opponents in an archery competition. The competition has set the following rules:
Alice first shoots numArrows arrows and then Bob shoots numArrows arrows.
The points are then calculated as follows:
The target has integer scoring sections ranging from 0 to 11 inclusive.
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.
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.
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 <= 105
aliceArrows.length == bobArrows.length == 12
0 <= aliceArrows[i], bobArrows[i] <= numArrows
sum(aliceArrows[i]) == numArrows
Complexity Analysis
- Time Complexity: O(2^{12} \cdot 12) = O(1)
- Space Complexity: O(2^{12}) = O(1)
2212. Maximum Points in an Archery Competition LeetCode Solution in C++
class Solution {
public:
vector<int> maximumBobPoints(int numArrows, vector<int>& aliceArrows) {
constexpr int kFullMask = (1 << 12) - 1;
int maxPoint = 0;
int maxMask = 0;
for (int mask = 0; mask < kFullMask; ++mask) {
const auto& [shotable, point] =
getShotableAndPoint(mask, numArrows, aliceArrows);
if (shotable && point > maxPoint) {
maxPoint = point;
maxMask = mask;
}
}
return getBobsArrows(maxMask, numArrows, aliceArrows);
}
private:
pair<bool, int> getShotableAndPoint(int mask, int leftArrows,
const vector<int>& aliceArrows) {
int point = 0;
for (int i = 0; i < 12; ++i)
if (mask >> i & 1) {
leftArrows -= aliceArrows[i] + 1;
point += i;
}
return {leftArrows >= 0, point};
}
vector<int> getBobsArrows(int mask, int leftArrows,
const vector<int>& aliceArrows) {
vector<int> bobsArrows(12);
for (int i = 0; i < 12; ++i)
if (mask >> i & 1) {
bobsArrows[i] = aliceArrows[i] + 1;
leftArrows -= aliceArrows[i] + 1;
}
bobsArrows[0] = leftArrows;
return bobsArrows;
}
};
/* code provided by PROGIEZ */
2212. Maximum Points in an Archery Competition LeetCode Solution in Java
class Solution {
public int[] maximumBobPoints(int numArrows, int[] aliceArrows) {
final int kFullMask = (1 << 12) - 1;
int maxPoint = 0;
int maxMask = 0;
for (int mask = 0; mask < kFullMask; ++mask) {
Pair<Boolean, Integer> pair = getShotableAndPoint(mask, numArrows, aliceArrows);
final boolean shotable = pair.getKey();
final int point = pair.getValue();
if (shotable && point > maxPoint) {
maxPoint = point;
maxMask = mask;
}
}
return getBobsArrows(maxMask, numArrows, aliceArrows);
}
private Pair<Boolean, Integer> getShotableAndPoint(int mask, int leftArrows, int[] aliceArrows) {
int point = 0;
for (int i = 0; i < 12; ++i)
if ((mask >> i & 1) == 1) {
leftArrows -= aliceArrows[i] + 1;
point += i;
}
return new Pair<>(leftArrows >= 0, point);
}
int[] getBobsArrows(int mask, int leftArrows, int[] aliceArrows) {
int[] bobsArrows = new int[12];
for (int i = 0; i < 12; ++i)
if ((mask >> i & 1) == 1) {
bobsArrows[i] = aliceArrows[i] + 1;
leftArrows -= aliceArrows[i] + 1;
}
bobsArrows[0] = leftArrows;
return bobsArrows;
}
}
// code provided by PROGIEZ
2212. Maximum Points in an Archery Competition LeetCode Solution in Python
class Solution:
def maximumBobPoints(
self,
numArrows: int,
aliceArrows: list[int],
) -> list[int]:
kFullMask = (1 << 12) - 1
maxPoint = 0
maxMask = 0
def getShotableAndPoint(mask: int, leftArrows: int) -> tuple[bool, int]:
point = 0
for i in range(12):
if mask >> i & 1:
leftArrows -= aliceArrows[i] + 1
point += i
return leftArrows >= 0, point
for mask in range(kFullMask):
shotable, point = getShotableAndPoint(mask, numArrows)
if shotable and point > maxPoint:
maxPoint = point
maxMask = mask
def getBobsArrows(mask: int, leftArrows: int) -> list[int]:
bobsArrows = [0] * 12
for i in range(12):
if mask >> i & 1:
bobsArrows[i] = aliceArrows[i] + 1
leftArrows -= aliceArrows[i] + 1
bobsArrows[0] = leftArrows
return bobsArrows
return getBobsArrows(maxMask, numArrows)
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.