< Summary

Information
Class: LeetCode.Algorithms.DividePlayersIntoTeamsOfEqualSkill.DividePlayersIntoTeamsOfEqualSkillDictionary
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\DividePlayersIntoTeamsOfEqualSkill\DividePlayersIntoTeamsOfEqualSkillDictionary.cs
Line coverage
92%
Covered lines: 24
Uncovered lines: 2
Coverable lines: 26
Total lines: 63
Line coverage: 92.3%
Branch coverage
90%
Covered branches: 9
Total branches: 10
Branch coverage: 90%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
DividePlayers(...)90%101092.3%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\DividePlayersIntoTeamsOfEqualSkill\DividePlayersIntoTeamsOfEqualSkillDictionary.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv).
 3// All Rights Reserved.
 4// --------------------------------------------------------------------------------
 5// This software is the confidential and proprietary information of Eugene Eremeev
 6// (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not
 7// disclose such Confidential Information and shall use it only in accordance with
 8// the terms of the license agreement you entered into with Eugene Eremeev (also
 9// known as Yevhenii Yeriemeieiv).
 10// --------------------------------------------------------------------------------
 11
 12namespace LeetCode.Algorithms.DividePlayersIntoTeamsOfEqualSkill;
 13
 14/// <inheritdoc />
 15public class DividePlayersIntoTeamsOfEqualSkillDictionary : IDividePlayersIntoTeamsOfEqualSkill
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n)
 19    ///     Space complexity - O(n)
 20    /// </summary>
 21    /// <param name="skill"></param>
 22    /// <returns></returns>
 23    public long DividePlayers(int[] skill)
 324    {
 1525        var totalSkill = skill.Aggregate<int, long>(0, (current, skillItem) => current + skillItem);
 26
 327        if (totalSkill % (skill.Length / 2) != 0)
 128        {
 129            return -1;
 30        }
 31
 232        var targetSkill = totalSkill / (skill.Length / 2);
 33
 234        var skillCountDictionary = new Dictionary<int, int>();
 35
 236        long totalChemistry = 0;
 37
 2238        foreach (var skillItem in skill)
 839        {
 840            var complement = (int)(targetSkill - skillItem);
 41
 842            if (skillCountDictionary.TryGetValue(complement, out var value) && value > 0)
 443            {
 444                totalChemistry += (long)skillItem * complement;
 45
 446                skillCountDictionary[complement]--;
 447            }
 48            else
 449            {
 450                skillCountDictionary.TryAdd(skillItem, 0);
 51
 452                skillCountDictionary[skillItem]++;
 453            }
 854        }
 55
 656        if (skillCountDictionary.Values.Any(count => count > 0))
 057        {
 058            return -1;
 59        }
 60
 261        return totalChemistry;
 362    }
 63}

Methods/Properties

DividePlayers(System.Int32[])