Skip to content

Commit 6ca0090

Browse files
committed
feat: add solutions to lc problem: No.2212
No.2212.Maximum Points in an Archery Competition
1 parent 7526455 commit 6ca0090

File tree

6 files changed

+355
-2
lines changed

6 files changed

+355
-2
lines changed

solution/2200-2299/2212.Maximum Points in an Archery Competition/README.md

Lines changed: 124 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,22 +72,145 @@ Bob 获得总分 8 + 9 + 10 = 27 。
7272

7373
<!-- 这里可写通用的实现逻辑 -->
7474

75+
**方法一:二进制枚举**
76+
77+
枚举 bob 射箭的最终状态,寻找满足题意的、且使得 bob 得分最大的状态。
78+
7579
<!-- tabs:start -->
7680

7781
### **Python3**
7882

7983
<!-- 这里可写当前语言的特殊实现逻辑 -->
8084

8185
```python
82-
86+
class Solution:
87+
def maximumBobPoints(self, numArrows: int, aliceArrows: List[int]) -> List[int]:
88+
n = len(aliceArrows)
89+
state = 0
90+
mx = -1
91+
for mask in range(1 << n):
92+
cnt = points = 0
93+
for i, alice in enumerate(aliceArrows):
94+
if (mask >> i) & 1:
95+
cnt += alice + 1
96+
points += i
97+
if cnt <= numArrows and mx < points:
98+
state = mask
99+
mx = points
100+
ans = [0] * n
101+
for i, alice in enumerate(aliceArrows):
102+
if (state >> i) & 1:
103+
ans[i] = alice + 1
104+
numArrows -= ans[i]
105+
ans[0] = numArrows
106+
return ans
83107
```
84108

85109
### **Java**
86110

87111
<!-- 这里可写当前语言的特殊实现逻辑 -->
88112

89113
```java
114+
class Solution {
115+
public int[] maximumBobPoints(int numArrows, int[] aliceArrows) {
116+
int n = aliceArrows.length;
117+
int mx = -1;
118+
int state = 0;
119+
for (int mask = 1; mask < 1 << n; ++mask) {
120+
int cnt = 0, points = 0;
121+
for (int i = 0; i < n; ++i) {
122+
if (((mask >> i) & 1) == 1) {
123+
cnt += aliceArrows[i] + 1;
124+
points += i;
125+
}
126+
}
127+
if (cnt <= numArrows && mx < points) {
128+
state = mask;
129+
mx = points;
130+
}
131+
}
132+
int[] ans = new int[n];
133+
for (int i = 0; i < n; ++i) {
134+
if (((state >> i) & 1) == 1) {
135+
ans[i] = aliceArrows[i] + 1;
136+
numArrows -= ans[i];
137+
}
138+
}
139+
ans[0] += numArrows;
140+
return ans;
141+
}
142+
}
143+
```
90144

145+
### **C++**
146+
147+
```cpp
148+
class Solution {
149+
public:
150+
vector<int> maximumBobPoints(int numArrows, vector<int>& aliceArrows) {
151+
int n = aliceArrows.size();
152+
int state = 0, mx = -1;
153+
for (int mask = 1; mask < 1 << n; ++mask)
154+
{
155+
int cnt = 0, points = 0;
156+
for (int i = 0; i < n; ++i)
157+
{
158+
if ((mask >> i) & 1)
159+
{
160+
cnt += aliceArrows[i] + 1;
161+
points += i;
162+
}
163+
}
164+
if (cnt <= numArrows && mx < points)
165+
{
166+
state = mask;
167+
mx = points;
168+
}
169+
}
170+
vector<int> ans(n);
171+
for (int i = 0; i < n; ++i)
172+
{
173+
if ((state >> i) & 1)
174+
{
175+
ans[i] = aliceArrows[i] + 1;
176+
numArrows -= ans[i];
177+
}
178+
}
179+
ans[0] += numArrows;
180+
return ans;
181+
}
182+
};
183+
```
184+
185+
### **Go**
186+
187+
```go
188+
func maximumBobPoints(numArrows int, aliceArrows []int) []int {
189+
n := len(aliceArrows)
190+
state, mx := 0, -1
191+
for mask := 1; mask < 1<<n; mask++ {
192+
cnt, points := 0, 0
193+
for i, alice := range aliceArrows {
194+
if (mask>>i)&1 == 1 {
195+
cnt += alice + 1
196+
points += i
197+
}
198+
}
199+
if cnt <= numArrows && mx < points {
200+
state = mask
201+
mx = points
202+
}
203+
}
204+
ans := make([]int, n)
205+
for i, alice := range aliceArrows {
206+
if (state>>i)&1 == 1 {
207+
ans[i] = alice + 1
208+
numArrows -= ans[i]
209+
}
210+
}
211+
ans[0] += numArrows
212+
return ans
213+
}
91214
```
92215

93216
### **TypeScript**

solution/2200-2299/2212.Maximum Points in an Archery Competition/README_EN.md

Lines changed: 120 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,132 @@ It can be shown that Bob cannot obtain a score higher than 27 points.
6767
### **Python3**
6868

6969
```python
70-
70+
class Solution:
71+
def maximumBobPoints(self, numArrows: int, aliceArrows: List[int]) -> List[int]:
72+
n = len(aliceArrows)
73+
state = 0
74+
mx = -1
75+
for mask in range(1 << n):
76+
cnt = points = 0
77+
for i, alice in enumerate(aliceArrows):
78+
if (mask >> i) & 1:
79+
cnt += alice + 1
80+
points += i
81+
if cnt <= numArrows and mx < points:
82+
state = mask
83+
mx = points
84+
ans = [0] * n
85+
for i, alice in enumerate(aliceArrows):
86+
if (state >> i) & 1:
87+
ans[i] = alice + 1
88+
numArrows -= ans[i]
89+
ans[0] = numArrows
90+
return ans
7191
```
7292

7393
### **Java**
7494

7595
```java
96+
class Solution {
97+
public int[] maximumBobPoints(int numArrows, int[] aliceArrows) {
98+
int n = aliceArrows.length;
99+
int mx = -1;
100+
int state = 0;
101+
for (int mask = 1; mask < 1 << n; ++mask) {
102+
int cnt = 0, points = 0;
103+
for (int i = 0; i < n; ++i) {
104+
if (((mask >> i) & 1) == 1) {
105+
cnt += aliceArrows[i] + 1;
106+
points += i;
107+
}
108+
}
109+
if (cnt <= numArrows && mx < points) {
110+
state = mask;
111+
mx = points;
112+
}
113+
}
114+
int[] ans = new int[n];
115+
for (int i = 0; i < n; ++i) {
116+
if (((state >> i) & 1) == 1) {
117+
ans[i] = aliceArrows[i] + 1;
118+
numArrows -= ans[i];
119+
}
120+
}
121+
ans[0] += numArrows;
122+
return ans;
123+
}
124+
}
125+
```
76126

127+
### **C++**
128+
129+
```cpp
130+
class Solution {
131+
public:
132+
vector<int> maximumBobPoints(int numArrows, vector<int>& aliceArrows) {
133+
int n = aliceArrows.size();
134+
int state = 0, mx = -1;
135+
for (int mask = 1; mask < 1 << n; ++mask)
136+
{
137+
int cnt = 0, points = 0;
138+
for (int i = 0; i < n; ++i)
139+
{
140+
if ((mask >> i) & 1)
141+
{
142+
cnt += aliceArrows[i] + 1;
143+
points += i;
144+
}
145+
}
146+
if (cnt <= numArrows && mx < points)
147+
{
148+
state = mask;
149+
mx = points;
150+
}
151+
}
152+
vector<int> ans(n);
153+
for (int i = 0; i < n; ++i)
154+
{
155+
if ((state >> i) & 1)
156+
{
157+
ans[i] = aliceArrows[i] + 1;
158+
numArrows -= ans[i];
159+
}
160+
}
161+
ans[0] += numArrows;
162+
return ans;
163+
}
164+
};
165+
```
166+
167+
### **Go**
168+
169+
```go
170+
func maximumBobPoints(numArrows int, aliceArrows []int) []int {
171+
n := len(aliceArrows)
172+
state, mx := 0, -1
173+
for mask := 1; mask < 1<<n; mask++ {
174+
cnt, points := 0, 0
175+
for i, alice := range aliceArrows {
176+
if (mask>>i)&1 == 1 {
177+
cnt += alice + 1
178+
points += i
179+
}
180+
}
181+
if cnt <= numArrows && mx < points {
182+
state = mask
183+
mx = points
184+
}
185+
}
186+
ans := make([]int, n)
187+
for i, alice := range aliceArrows {
188+
if (state>>i)&1 == 1 {
189+
ans[i] = alice + 1
190+
numArrows -= ans[i]
191+
}
192+
}
193+
ans[0] += numArrows
194+
return ans
195+
}
77196
```
78197

79198
### **TypeScript**
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public:
3+
vector<int> maximumBobPoints(int numArrows, vector<int>& aliceArrows) {
4+
int n = aliceArrows.size();
5+
int state = 0, mx = -1;
6+
for (int mask = 1; mask < 1 << n; ++mask)
7+
{
8+
int cnt = 0, points = 0;
9+
for (int i = 0; i < n; ++i)
10+
{
11+
if ((mask >> i) & 1)
12+
{
13+
cnt += aliceArrows[i] + 1;
14+
points += i;
15+
}
16+
}
17+
if (cnt <= numArrows && mx < points)
18+
{
19+
state = mask;
20+
mx = points;
21+
}
22+
}
23+
vector<int> ans(n);
24+
for (int i = 0; i < n; ++i)
25+
{
26+
if ((state >> i) & 1)
27+
{
28+
ans[i] = aliceArrows[i] + 1;
29+
numArrows -= ans[i];
30+
}
31+
}
32+
ans[0] += numArrows;
33+
return ans;
34+
}
35+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
func maximumBobPoints(numArrows int, aliceArrows []int) []int {
2+
n := len(aliceArrows)
3+
state, mx := 0, -1
4+
for mask := 1; mask < 1<<n; mask++ {
5+
cnt, points := 0, 0
6+
for i, alice := range aliceArrows {
7+
if (mask>>i)&1 == 1 {
8+
cnt += alice + 1
9+
points += i
10+
}
11+
}
12+
if cnt <= numArrows && mx < points {
13+
state = mask
14+
mx = points
15+
}
16+
}
17+
ans := make([]int, n)
18+
for i, alice := range aliceArrows {
19+
if (state>>i)&1 == 1 {
20+
ans[i] = alice + 1
21+
numArrows -= ans[i]
22+
}
23+
}
24+
ans[0] += numArrows
25+
return ans
26+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public int[] maximumBobPoints(int numArrows, int[] aliceArrows) {
3+
int n = aliceArrows.length;
4+
int mx = -1;
5+
int state = 0;
6+
for (int mask = 1; mask < 1 << n; ++mask) {
7+
int cnt = 0, points = 0;
8+
for (int i = 0; i < n; ++i) {
9+
if (((mask >> i) & 1) == 1) {
10+
cnt += aliceArrows[i] + 1;
11+
points += i;
12+
}
13+
}
14+
if (cnt <= numArrows && mx < points) {
15+
state = mask;
16+
mx = points;
17+
}
18+
}
19+
int[] ans = new int[n];
20+
for (int i = 0; i < n; ++i) {
21+
if (((state >> i) & 1) == 1) {
22+
ans[i] = aliceArrows[i] + 1;
23+
numArrows -= ans[i];
24+
}
25+
}
26+
ans[0] += numArrows;
27+
return ans;
28+
}
29+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
def maximumBobPoints(self, numArrows: int, aliceArrows: List[int]) -> List[int]:
3+
n = len(aliceArrows)
4+
state = 0
5+
mx = -1
6+
for mask in range(1 << n):
7+
cnt = points = 0
8+
for i, alice in enumerate(aliceArrows):
9+
if (mask >> i) & 1:
10+
cnt += alice + 1
11+
points += i
12+
if cnt <= numArrows and mx < points:
13+
state = mask
14+
mx = points
15+
ans = [0] * n
16+
for i, alice in enumerate(aliceArrows):
17+
if (state >> i) & 1:
18+
ans[i] = alice + 1
19+
numArrows -= ans[i]
20+
ans[0] = numArrows
21+
return ans

0 commit comments

Comments
 (0)