< Summary

Information
Class: LeetCode.Algorithms.NumberOfPeopleAwareOfSecret.NumberOfPeopleAwareOfSecretLineSweep
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\NumberOfPeopleAwareOfSecret\NumberOfPeopleAwareOfSecretLineSweep.cs
Line coverage
93%
Covered lines: 41
Uncovered lines: 3
Coverable lines: 44
Total lines: 91
Line coverage: 93.1%
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
PeopleAwareOfSecret(...)92.85%141493.18%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\NumberOfPeopleAwareOfSecret\NumberOfPeopleAwareOfSecretLineSweep.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2026 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.NumberOfPeopleAwareOfSecret;
 13
 14/// <inheritdoc />
 15public sealed class NumberOfPeopleAwareOfSecretLineSweep : INumberOfPeopleAwareOfSecret
 16{
 17    private const int Modulo = 1_000_000_007;
 18
 19    /// <summary>
 20    ///     Time complexity - O(n)
 21    ///     Space complexity - O(n)
 22    /// </summary>
 23    /// <param name="n"></param>
 24    /// <param name="delay"></param>
 25    /// <param name="forget"></param>
 26    /// <returns></returns>
 27    public int PeopleAwareOfSecret(int n, int delay, int forget)
 228    {
 229        var length = n + forget + 2;
 30
 231        Span<long> peopleWhoLearnOnDay = stackalloc long[length];
 32
 233        peopleWhoLearnOnDay[1] = 1;
 34
 235        Span<long> sharingDelta = stackalloc long[length];
 36
 237        long currentKnowCount = 1;
 38
 239        var personDelay = delay + 1;
 40
 241        if (personDelay < length)
 242        {
 243            sharingDelta[personDelay] = (sharingDelta[personDelay] + 1) % Modulo;
 244        }
 45
 246        var personForget = forget + 1;
 47
 248        if (personForget < length)
 249        {
 250            sharingDelta[personForget] = (sharingDelta[personForget] - 1 + Modulo) % Modulo;
 251        }
 52
 253        long currentSharers = 0;
 54
 2055        for (var day = 2; day <= n; day++)
 856        {
 857            currentSharers = (currentSharers + sharingDelta[day]) % Modulo;
 58
 859            peopleWhoLearnOnDay[day] = currentSharers;
 60
 861            currentKnowCount = (currentKnowCount + peopleWhoLearnOnDay[day]) % Modulo;
 62
 863            var forgetDay = day - forget;
 64
 865            if (forgetDay >= 1)
 366            {
 367                currentKnowCount = (currentKnowCount - peopleWhoLearnOnDay[forgetDay]) % Modulo;
 68
 369                if (currentKnowCount < 0)
 070                {
 071                    currentKnowCount += Modulo;
 072                }
 373            }
 74
 875            var shareStartDay = day + delay;
 876            var shareEndDay = day + forget;
 77
 878            if (shareStartDay < length)
 879            {
 880                sharingDelta[shareStartDay] = (sharingDelta[shareStartDay] + peopleWhoLearnOnDay[day]) % Modulo;
 881            }
 82
 883            if (shareEndDay < length)
 884            {
 885                sharingDelta[shareEndDay] = (sharingDelta[shareEndDay] - peopleWhoLearnOnDay[day] + Modulo) % Modulo;
 886            }
 887        }
 88
 289        return (int)(currentKnowCount % Modulo);
 290    }
 91}