< Summary

Information
Class: LeetCode.Algorithms.HandOfStraights.HandOfStraightsSortedDictionary
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\HandOfStraights\HandOfStraightsSortedDictionary.cs
Line coverage
100%
Covered lines: 34
Uncovered lines: 0
Coverable lines: 34
Total lines: 69
Line coverage: 100%
Branch coverage
100%
Covered branches: 14
Total branches: 14
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
IsNStraightHand(...)100%1414100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\HandOfStraights\HandOfStraightsSortedDictionary.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.HandOfStraights;
 13
 14/// <inheritdoc />
 15public class HandOfStraightsSortedDictionary : IHandOfStraights
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n log n)
 19    ///     Space complexity - O(n)
 20    /// </summary>
 21    /// <param name="hand"></param>
 22    /// <param name="groupSize"></param>
 23    /// <returns></returns>
 24    public bool IsNStraightHand(int[] hand, int groupSize)
 1525    {
 1526        if (hand.Length % groupSize != 0)
 327        {
 328            return false;
 29        }
 30
 1231        var cardsDictionary = new SortedDictionary<int, int>();
 32
 16833        foreach (var card in hand)
 6634        {
 6635            if (!cardsDictionary.TryAdd(card, 1))
 1036            {
 1037                cardsDictionary[card]++;
 1038            }
 6639        }
 40
 3341        while (cardsDictionary.Count > 0)
 2342        {
 2343            var firstCard = cardsDictionary.First().Key;
 44
 16845            for (var i = 0; i < groupSize; i++)
 6346            {
 6347                var currentCard = firstCard + i;
 48
 6349                if (cardsDictionary.TryGetValue(currentCard, out var value))
 6150                {
 6151                    if (value == 1)
 5352                    {
 5353                        cardsDictionary.Remove(currentCard);
 5354                    }
 55                    else
 856                    {
 857                        cardsDictionary[currentCard] = value - 1;
 858                    }
 6159                }
 60                else
 261                {
 262                    return false;
 63                }
 6164            }
 2165        }
 66
 1067        return true;
 1568    }
 69}