Skip to content

Commit 7bfc65a

Browse files
authored
feat: add solutions to lc problem: No.2212 (#4090)
No.2212.Maximum Points in an Archery Competition
1 parent 758e622 commit 7bfc65a

File tree

9 files changed

+433
-304
lines changed

9 files changed

+433
-304
lines changed

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

+146-100
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,13 @@ Bob 获得总分 8 + 9 + 10 = 27 。
9191

9292
### 方法一:二进制枚举
9393

94-
枚举 bob 射箭的最终状态,寻找满足题意的、且使得 bob 得分最大的状态。
94+
由于区域数目只有 $12$ 个,因此我们使用二进制枚举的方式,枚举 $\textit{Bob}$ 在哪些区域得分。用一个变量 $\textit{st}$ 表示 $\textit{Bob}$ 获得最大得分的方案,而 $\textit{mx}$ 表示 $\textit{Bob}$ 获得的最大得分。
95+
96+
我们在 $[1, 2^m)$ 的区间内枚举 $\textit{Bob}$ 的得分方案,其中 $m$ 是 $\textit{aliceArrows}$ 的长度。对于每一个方案,我们计算 $\textit{Bob}$ 的得分 $\textit{s}$ 以及射箭的数量 $\textit{cnt}$。如果 $\textit{cnt} \leq \textit{numArrows}$ 且 $\textit{s} > \textit{mx}$,我们就更新 $\textit{mx}$ 和 $\textit{st}$。
97+
98+
然后,我们根据 $\textit{st}$ 计算 $\textit{Bob}$ 的得分方案,如果最后还有剩余的射箭,我们将剩余的射箭分配给第一个区域,即下标为 $0$ 的区域。
99+
100+
时间复杂度 $O(2^m \times m)$,其中 $m$ 是 $\textit{aliceArrows}$ 的长度。忽略答案数组的空间消耗,空间复杂度 $O(1)$。
95101

96102
<!-- tabs:start -->
97103

@@ -100,24 +106,23 @@ Bob 获得总分 8 + 9 + 10 = 27 。
100106
```python
101107
class Solution:
102108
def maximumBobPoints(self, numArrows: int, aliceArrows: List[int]) -> List[int]:
103-
n = len(aliceArrows)
104-
state = 0
105-
mx = -1
106-
for mask in range(1 << n):
107-
cnt = points = 0
108-
for i, alice in enumerate(aliceArrows):
109-
if (mask >> i) & 1:
110-
cnt += alice + 1
111-
points += i
112-
if cnt <= numArrows and mx < points:
113-
state = mask
114-
mx = points
115-
ans = [0] * n
116-
for i, alice in enumerate(aliceArrows):
117-
if (state >> i) & 1:
118-
ans[i] = alice + 1
109+
st = mx = 0
110+
m = len(aliceArrows)
111+
for mask in range(1, 1 << m):
112+
cnt = s = 0
113+
for i, x in enumerate(aliceArrows):
114+
if mask >> i & 1:
115+
s += i
116+
cnt += x + 1
117+
if cnt <= numArrows and s > mx:
118+
mx = s
119+
st = mask
120+
ans = [0] * m
121+
for i, x in enumerate(aliceArrows):
122+
if st >> i & 1:
123+
ans[i] = x + 1
119124
numArrows -= ans[i]
120-
ans[0] = numArrows
125+
ans[0] += numArrows
121126
return ans
122127
```
123128

@@ -126,25 +131,24 @@ class Solution:
126131
```java
127132
class Solution {
128133
public int[] maximumBobPoints(int numArrows, int[] aliceArrows) {
129-
int n = aliceArrows.length;
130-
int mx = -1;
131-
int state = 0;
132-
for (int mask = 1; mask < 1 << n; ++mask) {
133-
int cnt = 0, points = 0;
134-
for (int i = 0; i < n; ++i) {
135-
if (((mask >> i) & 1) == 1) {
134+
int st = 0, mx = 0;
135+
int m = aliceArrows.length;
136+
for (int mask = 1; mask < 1 << m; ++mask) {
137+
int cnt = 0, s = 0;
138+
for (int i = 0; i < m; ++i) {
139+
if ((mask >> i & 1) == 1) {
140+
s += i;
136141
cnt += aliceArrows[i] + 1;
137-
points += i;
138142
}
139143
}
140-
if (cnt <= numArrows && mx < points) {
141-
state = mask;
142-
mx = points;
144+
if (cnt <= numArrows && s > mx) {
145+
mx = s;
146+
st = mask;
143147
}
144148
}
145-
int[] ans = new int[n];
146-
for (int i = 0; i < n; ++i) {
147-
if (((state >> i) & 1) == 1) {
149+
int[] ans = new int[m];
150+
for (int i = 0; i < m; ++i) {
151+
if ((st >> i & 1) == 1) {
148152
ans[i] = aliceArrows[i] + 1;
149153
numArrows -= ans[i];
150154
}
@@ -161,24 +165,24 @@ class Solution {
161165
class Solution {
162166
public:
163167
vector<int> maximumBobPoints(int numArrows, vector<int>& aliceArrows) {
164-
int n = aliceArrows.size();
165-
int state = 0, mx = -1;
166-
for (int mask = 1; mask < 1 << n; ++mask) {
167-
int cnt = 0, points = 0;
168-
for (int i = 0; i < n; ++i) {
169-
if ((mask >> i) & 1) {
168+
int st = 0, mx = 0;
169+
int m = aliceArrows.size();
170+
for (int mask = 1; mask < 1 << m; ++mask) {
171+
int cnt = 0, s = 0;
172+
for (int i = 0; i < m; ++i) {
173+
if (mask >> i & 1) {
174+
s += i;
170175
cnt += aliceArrows[i] + 1;
171-
points += i;
172176
}
173177
}
174-
if (cnt <= numArrows && mx < points) {
175-
state = mask;
176-
mx = points;
178+
if (cnt <= numArrows && s > mx) {
179+
mx = s;
180+
st = mask;
177181
}
178182
}
179-
vector<int> ans(n);
180-
for (int i = 0; i < n; ++i) {
181-
if ((state >> i) & 1) {
183+
vector<int> ans(m);
184+
for (int i = 0; i < m; ++i) {
185+
if (st >> i & 1) {
182186
ans[i] = aliceArrows[i] + 1;
183187
numArrows -= ans[i];
184188
}
@@ -193,25 +197,25 @@ public:
193197
194198
```go
195199
func maximumBobPoints(numArrows int, aliceArrows []int) []int {
196-
n := len(aliceArrows)
197-
state, mx := 0, -1
198-
for mask := 1; mask < 1<<n; mask++ {
199-
cnt, points := 0, 0
200-
for i, alice := range aliceArrows {
201-
if (mask>>i)&1 == 1 {
202-
cnt += alice + 1
203-
points += i
200+
st, mx := 0, 0
201+
m := len(aliceArrows)
202+
for mask := 1; mask < 1<<m; mask++ {
203+
cnt, s := 0, 0
204+
for i, x := range aliceArrows {
205+
if mask>>i&1 == 1 {
206+
s += i
207+
cnt += x + 1
204208
}
205209
}
206-
if cnt <= numArrows && mx < points {
207-
state = mask
208-
mx = points
210+
if cnt <= numArrows && s > mx {
211+
mx = s
212+
st = mask
209213
}
210214
}
211-
ans := make([]int, n)
212-
for i, alice := range aliceArrows {
213-
if (state>>i)&1 == 1 {
214-
ans[i] = alice + 1
215+
ans := make([]int, m)
216+
for i, x := range aliceArrows {
217+
if (st>>i)&1 == 1 {
218+
ans[i] = x + 1
215219
numArrows -= ans[i]
216220
}
217221
}
@@ -224,61 +228,103 @@ func maximumBobPoints(numArrows int, aliceArrows []int) []int {
224228

225229
```ts
226230
function maximumBobPoints(numArrows: number, aliceArrows: number[]): number[] {
227-
const dfs = (arr: number[], i: number, c: number): number[] => {
228-
if (i < 0 || c === 0) {
229-
arr[0] += c;
230-
return arr;
231-
}
232-
const a1 = dfs([...arr], i - 1, c);
233-
if (c > aliceArrows[i]) {
234-
arr[i] = aliceArrows[i] + 1;
235-
const a2 = dfs(arr, i - 1, c - aliceArrows[i] - 1);
236-
if (
237-
a2.reduce((p, v, i) => p + (v > 0 ? i : 0), 0) >=
238-
a1.reduce((p, v, i) => p + (v > 0 ? i : 0), 0)
239-
) {
240-
return a2;
231+
let [st, mx] = [0, 0];
232+
const m = aliceArrows.length;
233+
for (let mask = 1; mask < 1 << m; mask++) {
234+
let [cnt, s] = [0, 0];
235+
for (let i = 0; i < m; i++) {
236+
if ((mask >> i) & 1) {
237+
cnt += aliceArrows[i] + 1;
238+
s += i;
241239
}
242240
}
243-
return a1;
244-
};
245-
return dfs(new Array(12).fill(0), 11, numArrows);
241+
if (cnt <= numArrows && s > mx) {
242+
mx = s;
243+
st = mask;
244+
}
245+
}
246+
const ans: number[] = Array(m).fill(0);
247+
for (let i = 0; i < m; i++) {
248+
if ((st >> i) & 1) {
249+
ans[i] = aliceArrows[i] + 1;
250+
numArrows -= ans[i];
251+
}
252+
}
253+
ans[0] += numArrows;
254+
return ans;
246255
}
247256
```
248257

249258
#### Rust
250259

251260
```rust
252261
impl Solution {
253-
fn dfs(alice_arrows: &Vec<i32>, mut res: Vec<i32>, count: i32, i: usize) -> Vec<i32> {
254-
if i == 0 || count == 0 {
255-
res[0] += count;
256-
return res;
262+
pub fn maximum_bob_points(num_arrows: i32, alice_arrows: Vec<i32>) -> Vec<i32> {
263+
let mut st = 0;
264+
let mut mx = 0;
265+
let m = alice_arrows.len();
266+
for mask in 1..(1 << m) {
267+
let mut cnt = 0;
268+
let mut s = 0;
269+
for i in 0..m {
270+
if (mask >> i) & 1 == 1 {
271+
s += i as i32;
272+
cnt += alice_arrows[i] + 1;
273+
}
274+
}
275+
if cnt <= num_arrows && s > mx {
276+
mx = s;
277+
st = mask;
278+
}
257279
}
258-
let r1 = Self::dfs(alice_arrows, res.clone(), count, i - 1);
259-
if count > alice_arrows[i] {
260-
res[i] = alice_arrows[i] + 1;
261-
let r2 = Self::dfs(alice_arrows, res, count - alice_arrows[i] - 1, i - 1);
262-
if r2
263-
.iter()
264-
.enumerate()
265-
.map(|(i, v)| if v > &0 { i } else { 0 })
266-
.sum::<usize>()
267-
> r1.iter()
268-
.enumerate()
269-
.map(|(i, v)| if v > &0 { i } else { 0 })
270-
.sum::<usize>()
271-
{
272-
return r2;
280+
let mut ans = vec![0; m];
281+
let mut num_arrows = num_arrows;
282+
for i in 0..m {
283+
if (st >> i) & 1 == 1 {
284+
ans[i] = alice_arrows[i] + 1;
285+
num_arrows -= ans[i];
273286
}
274287
}
275-
r1
288+
ans[0] += num_arrows;
289+
ans
276290
}
291+
}
292+
```
277293

278-
pub fn maximum_bob_points(num_arrows: i32, alice_arrows: Vec<i32>) -> Vec<i32> {
279-
Self::dfs(&alice_arrows, vec![0; 12], num_arrows, 11)
294+
#### JavaScript
295+
296+
```js
297+
/**
298+
* @param {number} numArrows
299+
* @param {number[]} aliceArrows
300+
* @return {number[]}
301+
*/
302+
var maximumBobPoints = function (numArrows, aliceArrows) {
303+
let [st, mx] = [0, 0];
304+
const m = aliceArrows.length;
305+
for (let mask = 1; mask < 1 << m; mask++) {
306+
let [cnt, s] = [0, 0];
307+
for (let i = 0; i < m; i++) {
308+
if ((mask >> i) & 1) {
309+
cnt += aliceArrows[i] + 1;
310+
s += i;
311+
}
312+
}
313+
if (cnt <= numArrows && s > mx) {
314+
mx = s;
315+
st = mask;
316+
}
280317
}
281-
}
318+
const ans = Array(m).fill(0);
319+
for (let i = 0; i < m; i++) {
320+
if ((st >> i) & 1) {
321+
ans[i] = aliceArrows[i] + 1;
322+
numArrows -= ans[i];
323+
}
324+
}
325+
ans[0] += numArrows;
326+
return ans;
327+
};
282328
```
283329

284330
<!-- tabs:end -->

0 commit comments

Comments
 (0)