< Summary

Information
Class: LeetCode.Algorithms.DistributeCandies.DistributeCandiesLookupSpan
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\DistributeCandies\DistributeCandiesLookupSpan.cs
Line coverage
100%
Covered lines: 21
Uncovered lines: 0
Coverable lines: 21
Total lines: 62
Line coverage: 100%
Branch coverage
100%
Covered branches: 6
Total branches: 6
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
DistributeCandies(...)100%66100%
GetIndex(...)100%11100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\DistributeCandies\DistributeCandiesLookupSpan.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.DistributeCandies;
 13
 14/// <inheritdoc />
 15public sealed class DistributeCandiesLookupSpan : IDistributeCandies
 16{
 17    private const int MinCandyType = -100_000;
 18    private const int MaxCandyType = 100_000;
 19    private const int Offset = -MinCandyType;
 20    private const int LookupSize = MaxCandyType - MinCandyType + 1;
 21
 22    /// <summary>
 23    ///     Time complexity - O(n)
 24    ///     Space complexity - O(1)
 25    /// </summary>
 26    /// <param name="candyTypes"></param>
 27    /// <returns></returns>
 28    public int DistributeCandies(int[] candyTypes)
 2029    {
 2030        var maxCount = candyTypes.Length / 2;
 31
 2032        Span<bool> candyTypesLookup = stackalloc bool[LookupSize];
 33
 2034        var uniqueCount = 0;
 35
 19836        foreach (var candyType in candyTypes)
 7837        {
 7838            var index = GetIndex(candyType);
 39
 7840            if (candyTypesLookup[index])
 2941            {
 2942                continue;
 43            }
 44
 4945            candyTypesLookup[index] = true;
 46
 4947            uniqueCount++;
 48
 4949            if (uniqueCount == maxCount)
 1850            {
 1851                return maxCount;
 52            }
 3153        }
 54
 255        return uniqueCount;
 2056    }
 57
 58    private static int GetIndex(int candyType)
 7859    {
 7860        return candyType + Offset;
 7861    }
 62}