< Summary

Information
Class: LeetCode.Algorithms.HandOfStraights.HandOfStraightsDictionary
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\HandOfStraights\HandOfStraightsDictionary.cs
Line coverage
100%
Covered lines: 35
Uncovered lines: 0
Coverable lines: 35
Total lines: 71
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\HandOfStraightsDictionary.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 HandOfStraightsDictionary : 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        Array.Sort(hand);
 32
 1233        var cardsDictionary = new Dictionary<int, int>();
 34
 16835        foreach (var card in hand)
 6636        {
 6637            if (!cardsDictionary.TryAdd(card, 1))
 1038            {
 1039                cardsDictionary[card]++;
 1040            }
 6641        }
 42
 3343        while (cardsDictionary.Count > 0)
 2344        {
 2345            var firstCard = cardsDictionary.First().Key;
 46
 16847            for (var i = 0; i < groupSize; i++)
 6348            {
 6349                var currentCard = firstCard + i;
 50
 6351                if (cardsDictionary.TryGetValue(currentCard, out var value))
 6152                {
 6153                    if (value == 1)
 5354                    {
 5355                        cardsDictionary.Remove(currentCard);
 5356                    }
 57                    else
 858                    {
 859                        cardsDictionary[currentCard] = value - 1;
 860                    }
 6161                }
 62                else
 263                {
 264                    return false;
 65                }
 6166            }
 2167        }
 68
 1069        return true;
 1570    }
 71}