1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-10-18 14:52:09 +02:00

cs: add «2491. Divide Players Into Teams of Equal Skill»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-10-04 21:18:47 +02:00
parent 5e5928bd8a
commit dd5ec27bb2
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View 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;
}
}