< Summary

Information
Class: LeetCode.Algorithms.New21Game.New21GameDynamicProgrammingSlidingWindow
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\New21Game\New21GameDynamicProgrammingSlidingWindow.cs
Line coverage
100%
Covered lines: 27
Uncovered lines: 0
Coverable lines: 27
Total lines: 63
Line coverage: 100%
Branch coverage
92%
Covered branches: 13
Total branches: 14
Branch coverage: 92.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
New21Game(...)92.85%1414100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\New21Game\New21GameDynamicProgrammingSlidingWindow.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 New21GameDynamicProgrammingSlidingWindow : INew21Game
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n)
 19    ///     Space complexity - O(n)
 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 dp = new double[n + 1];
 33
 234        dp[0] = 1.0;
 35
 236        var windowSum = 0.0;
 237        var result = 0.0;
 38
 5839        for (var score = 1; score <= n; score++)
 2740        {
 2741            if (score - 1 < k)
 1842            {
 1843                windowSum += dp[score - 1];
 1844            }
 45
 2746            var outgoing = score - 1 - maxPts;
 47
 2748            if (outgoing >= 0 && outgoing < k)
 1149            {
 1150                windowSum -= dp[outgoing];
 1151            }
 52
 2753            dp[score] = windowSum / maxPts;
 54
 2755            if (score >= k)
 1156            {
 1157                result += dp[score];
 1158            }
 2759        }
 60
 261        return result;
 362    }
 63}