cs: add «2491. Divide Players Into Teams of Equal Skill»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
5e5928bd8a
commit
dd5ec27bb2
1 changed files with 18 additions and 0 deletions
18
cs/divide-players-into-teams-of-equal-skill.cs
Normal file
18
cs/divide-players-into-teams-of-equal-skill.cs
Normal file
|
@ -0,0 +1,18 @@
|
|||
public class Solution {
|
||||
public long DividePlayers(int[] skill) {
|
||||
Array.Sort(skill);
|
||||
|
||||
var expectedSkill = skill[0] + skill[skill.Length - 1];
|
||||
|
||||
long chemistry = 0;
|
||||
for (int i = 0, j = skill.Length - 1; i < j; ++i, --j) {
|
||||
if (skill[i] + skill[j] != expectedSkill) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
chemistry += skill[i] * (long)skill[j];
|
||||
}
|
||||
|
||||
return chemistry;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue