Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,58 @@ func dividePlayers(skill []int) int64 {
}
```

#### TypeScript

```ts
function dividePlayers(skill: number[]): number {
let [sum, res, map] = [0, 0, new Map<number, number>()];

for (const x of skill) {
sum += x;
map.set(x, (map.get(x) || 0) + 1);
}
sum /= skill.length / 2;

for (let [x, c] of map) {
const complement = sum - x;
if ((map.get(complement) ?? 0) !== c) return -1;
if (x === complement) c /= 2;

res += x * complement * c;
map.delete(x);
map.delete(complement);
}

return res;
}
```

#### JavaScript

```js
function dividePlayers(skill) {
let [sum, res, map] = [0, 0, new Map()];

for (const x of skill) {
sum += x;
map.set(x, (map.get(x) || 0) + 1);
}
sum /= skill.length / 2;

for (let [x, c] of map) {
const complement = sum - x;
if ((map.get(complement) ?? 0) !== c) return -1;
if (x === complement) c /= 2;

res += x * complement * c;
map.delete(x);
map.delete(complement);
}

return res;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ tags:
<pre>
<strong>Input:</strong> skill = [3,2,5,1,3,4]
<strong>Output:</strong> 22
<strong>Explanation:</strong>
<strong>Explanation:</strong>
Divide the players into the following teams: (1, 5), (2, 4), (3, 3), where each team has a total skill of 6.
The sum of the chemistry of all the teams is: 1 * 5 + 2 * 4 + 3 * 3 = 5 + 8 + 9 = 22.
</pre>
Expand All @@ -43,7 +43,7 @@ The sum of the chemistry of all the teams is: 1 * 5 + 2 * 4 + 3 * 3 = 5 + 8 + 9
<pre>
<strong>Input:</strong> skill = [3,4]
<strong>Output:</strong> 12
<strong>Explanation:</strong>
<strong>Explanation:</strong>
The two players form a team with a total skill of 7.
The chemistry of the team is 3 * 4 = 12.
</pre>
Expand All @@ -53,7 +53,7 @@ The chemistry of the team is 3 * 4 = 12.
<pre>
<strong>Input:</strong> skill = [1,1,2,3]
<strong>Output:</strong> -1
<strong>Explanation:</strong>
<strong>Explanation:</strong>
There is no way to divide the players into teams such that the total skill of each team is equal.
</pre>

Expand Down Expand Up @@ -332,6 +332,58 @@ func dividePlayers(skill []int) int64 {
}
```

#### TypeScript

```ts
function dividePlayers(skill: number[]): number {
let [sum, res, map] = [0, 0, new Map<number, number>()];

for (const x of skill) {
sum += x;
map.set(x, (map.get(x) || 0) + 1);
}
sum /= skill.length / 2;

for (let [x, c] of map) {
const complement = sum - x;
if ((map.get(complement) ?? 0) !== c) return -1;
if (x === complement) c /= 2;

res += x * complement * c;
map.delete(x);
map.delete(complement);
}

return res;
}
```

#### JavaScript

```js
function dividePlayers(skill) {
let [sum, res, map] = [0, 0, new Map()];

for (const x of skill) {
sum += x;
map.set(x, (map.get(x) || 0) + 1);
}
sum /= skill.length / 2;

for (let [x, c] of map) {
const complement = sum - x;
if ((map.get(complement) ?? 0) !== c) return -1;
if (x === complement) c /= 2;

res += x * complement * c;
map.delete(x);
map.delete(complement);
}

return res;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function dividePlayers(skill) {
let [sum, res, map] = [0, 0, new Map()];

for (const x of skill) {
sum += x;
map.set(x, (map.get(x) || 0) + 1);
}
sum /= skill.length / 2;

for (let [x, c] of map) {
const complement = sum - x;
if ((map.get(complement) ?? 0) !== c) return -1;
if (x === complement) c /= 2;

res += x * complement * c;
map.delete(x);
map.delete(complement);
}

return res;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function dividePlayers(skill: number[]): number {
let [sum, res, map] = [0, 0, new Map<number, number>()];

for (const x of skill) {
sum += x;
map.set(x, (map.get(x) || 0) + 1);
}
sum /= skill.length / 2;

for (let [x, c] of map) {
const complement = sum - x;
if ((map.get(complement) ?? 0) !== c) return -1;
if (x === complement) c /= 2;

res += x * complement * c;
map.delete(x);
map.delete(complement);
}

return res;
}
Loading