Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit eb6ca64

Browse files
authored
feat: add solutions to lc problem: No.2491 (doocs#3601)
1 parent 23290a0 commit eb6ca64

File tree

4 files changed

+149
-3
lines changed

4 files changed

+149
-3
lines changed

‎solution/2400-2499/2491.Divide Players Into Teams of Equal Skill/README.md‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,58 @@ func dividePlayers(skill []int) int64 {
334334
}
335335
```
336336

337+
#### TypeScript
338+
339+
```ts
340+
function dividePlayers(skill: number[]): number {
341+
let [sum, res, map] = [0, 0, new Map<number, number>()];
342+
343+
for (const x of skill) {
344+
sum += x;
345+
map.set(x, (map.get(x) || 0) + 1);
346+
}
347+
sum /= skill.length / 2;
348+
349+
for (let [x, c] of map) {
350+
const complement = sum - x;
351+
if ((map.get(complement) ?? 0) !== c) return -1;
352+
if (x === complement) c /= 2;
353+
354+
res += x * complement * c;
355+
map.delete(x);
356+
map.delete(complement);
357+
}
358+
359+
return res;
360+
}
361+
```
362+
363+
#### JavaScript
364+
365+
```js
366+
function dividePlayers(skill) {
367+
let [sum, res, map] = [0, 0, new Map()];
368+
369+
for (const x of skill) {
370+
sum += x;
371+
map.set(x, (map.get(x) || 0) + 1);
372+
}
373+
sum /= skill.length / 2;
374+
375+
for (let [x, c] of map) {
376+
const complement = sum - x;
377+
if ((map.get(complement) ?? 0) !== c) return -1;
378+
if (x === complement) c /= 2;
379+
380+
res += x * complement * c;
381+
map.delete(x);
382+
map.delete(complement);
383+
}
384+
385+
return res;
386+
}
387+
```
388+
337389
<!-- tabs:end -->
338390
339391
<!-- solution:end -->

‎solution/2400-2499/2491.Divide Players Into Teams of Equal Skill/README_EN.md‎

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ tags:
3333
<pre>
3434
<strong>Input:</strong> skill = [3,2,5,1,3,4]
3535
<strong>Output:</strong> 22
36-
<strong>Explanation:</strong>
36+
<strong>Explanation:</strong>
3737
Divide the players into the following teams: (1, 5), (2, 4), (3, 3), where each team has a total skill of 6.
3838
The sum of the chemistry of all the teams is: 1 * 5 + 2 * 4 + 3 * 3 = 5 +たす 8 +たす 9 = 22.
3939
</pre>
@@ -43,7 +43,7 @@ The sum of the chemistry of all the teams is: 1 * 5 + 2 * 4 + 3 * 3 = 5 + 8 + 9
4343
<pre>
4444
<strong>Input:</strong> skill = [3,4]
4545
<strong>Output:</strong> 12
46-
<strong>Explanation:</strong>
46+
<strong>Explanation:</strong>
4747
The two players form a team with a total skill of 7.
4848
The chemistry of the team is 3 * 4 = 12.
4949
</pre>
@@ -53,7 +53,7 @@ The chemistry of the team is 3 * 4 = 12.
5353
<pre>
5454
<strong>Input:</strong> skill = [1,1,2,3]
5555
<strong>Output:</strong> -1
56-
<strong>Explanation:</strong>
56+
<strong>Explanation:</strong>
5757
There is no way to divide the players into teams such that the total skill of each team is equal.
5858
</pre>
5959

@@ -332,6 +332,58 @@ func dividePlayers(skill []int) int64 {
332332
}
333333
```
334334

335+
#### TypeScript
336+
337+
```ts
338+
function dividePlayers(skill: number[]): number {
339+
let [sum, res, map] = [0, 0, new Map<number, number>()];
340+
341+
for (const x of skill) {
342+
sum += x;
343+
map.set(x, (map.get(x) || 0) + 1);
344+
}
345+
sum /= skill.length / 2;
346+
347+
for (let [x, c] of map) {
348+
const complement = sum - x;
349+
if ((map.get(complement) ?? 0) !== c) return -1;
350+
if (x === complement) c /= 2;
351+
352+
res += x * complement * c;
353+
map.delete(x);
354+
map.delete(complement);
355+
}
356+
357+
return res;
358+
}
359+
```
360+
361+
#### JavaScript
362+
363+
```js
364+
function dividePlayers(skill) {
365+
let [sum, res, map] = [0, 0, new Map()];
366+
367+
for (const x of skill) {
368+
sum += x;
369+
map.set(x, (map.get(x) || 0) + 1);
370+
}
371+
sum /= skill.length / 2;
372+
373+
for (let [x, c] of map) {
374+
const complement = sum - x;
375+
if ((map.get(complement) ?? 0) !== c) return -1;
376+
if (x === complement) c /= 2;
377+
378+
res += x * complement * c;
379+
map.delete(x);
380+
map.delete(complement);
381+
}
382+
383+
return res;
384+
}
385+
```
386+
335387
<!-- tabs:end -->
336388
337389
<!-- solution:end -->
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function dividePlayers(skill) {
2+
let [sum, res, map] = [0, 0, new Map()];
3+
4+
for (const x of skill) {
5+
sum += x;
6+
map.set(x, (map.get(x) || 0) + 1);
7+
}
8+
sum /= skill.length / 2;
9+
10+
for (let [x, c] of map) {
11+
const complement = sum - x;
12+
if ((map.get(complement) ?? 0) !== c) return -1;
13+
if (x === complement) c /= 2;
14+
15+
res += x * complement * c;
16+
map.delete(x);
17+
map.delete(complement);
18+
}
19+
20+
return res;
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function dividePlayers(skill: number[]): number {
2+
let [sum, res, map] = [0, 0, new Map<number, number>()];
3+
4+
for (const x of skill) {
5+
sum += x;
6+
map.set(x, (map.get(x) || 0) + 1);
7+
}
8+
sum /= skill.length / 2;
9+
10+
for (let [x, c] of map) {
11+
const complement = sum - x;
12+
if ((map.get(complement) ?? 0) !== c) return -1;
13+
if (x === complement) c /= 2;
14+
15+
res += x * complement * c;
16+
map.delete(x);
17+
map.delete(complement);
18+
}
19+
20+
return res;
21+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /