< Summary

Information
Class: LeetCode.Algorithms.New21Game.New21GameDictionaryPriorityQueue
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\New21Game\New21GameDictionaryPriorityQueue.cs
Line coverage
100%
Covered lines: 48
Uncovered lines: 0
Coverable lines: 48
Total lines: 90
Line coverage: 100%
Branch coverage
95%
Covered branches: 21
Total branches: 22
Branch coverage: 95.4%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
New21Game(...)95.45%2222100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\New21Game\New21GameDictionaryPriorityQueue.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.New21Game;
 13
 14/// <inheritdoc />
 15public class New21GameDictionaryPriorityQueue : INew21Game
 16{
 17    /// <summary>
 18    ///     Time complexity - O(k * maxPts * log k)
 19    ///     Space complexity - O(k)
 20    /// </summary>
 21    /// <param name="n"></param>
 22    /// <param name="k"></param>
 23    /// <param name="maxPts"></param>
 24    /// <returns></returns>
 25    public double New21Game(int n, int k, int maxPts)
 326    {
 327        if (k == 0 || n >= k - 1 + maxPts)
 128        {
 129            return 1;
 30        }
 31
 232        var result = 0.0;
 33
 234        var probabilityByScoreDictionary = new Dictionary<int, double>();
 35
 4436        for (var point = 1; point <= maxPts; point++)
 2037        {
 2038            probabilityByScoreDictionary.Add(point, 1.0 / maxPts);
 2039        }
 40
 241        var scoresPriorityQueue = new PriorityQueue<int, int>();
 42
 4643        foreach (var (score, probability) in probabilityByScoreDictionary)
 2044        {
 2045            if (score < k)
 1046            {
 1047                scoresPriorityQueue.Enqueue(score, score);
 1048            }
 1049            else if (score <= n)
 650            {
 651                result += probability;
 652            }
 2053        }
 54
 11755        while (scoresPriorityQueue.Count > 0)
 11556        {
 11557            var currentScore = scoresPriorityQueue.Dequeue();
 58
 11559            var currentProbability = probabilityByScoreDictionary[currentScore];
 60
 11561            if (currentProbability == 0.0)
 9962            {
 9963                continue;
 64            }
 65
 1666            probabilityByScoreDictionary[currentScore] = 0.0;
 67
 1668            var probabilityPerDraw = currentProbability / maxPts;
 69
 35270            for (var drawValue = 1; drawValue <= maxPts; drawValue++)
 16071            {
 16072                var nextScore = currentScore + drawValue;
 73
 16074                if (nextScore < k)
 10575                {
 10576                    probabilityByScoreDictionary.TryGetValue(nextScore, out var oldProbability);
 10577                    probabilityByScoreDictionary[nextScore] = oldProbability + probabilityPerDraw;
 78
 10579                    scoresPriorityQueue.Enqueue(nextScore, nextScore);
 10580                }
 5581                else if (nextScore <= n)
 4082                {
 4083                    result += probabilityPerDraw;
 4084                }
 16085            }
 1686        }
 87
 288        return result;
 389    }
 90}