< Summary

Information
Class: LeetCode.Algorithms.ShortestSubarrayWithORAtLeastK2.ShortestSubarrayWithORAtLeastK2SlidingWindow
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ShortestSubarrayWithORAtLeastK2\ShortestSubarrayWithORAtLeastK2SlidingWindow.cs
Line coverage
100%
Covered lines: 38
Uncovered lines: 0
Coverable lines: 38
Total lines: 75
Line coverage: 100%
Branch coverage
93%
Covered branches: 15
Total branches: 16
Branch coverage: 93.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
MinimumSubarrayLength(...)87.5%88100%
UpdateBitCounts(...)100%44100%
ConvertBitCountsToNumber(...)100%44100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ShortestSubarrayWithORAtLeastK2\ShortestSubarrayWithORAtLeastK2SlidingWindow.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.ShortestSubarrayWithORAtLeastK2;
 13
 14/// <inheritdoc />
 15public class ShortestSubarrayWithORAtLeastK2SlidingWindow : IShortestSubarrayWithORAtLeastK2
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n)
 19    ///     Space complexity - O(1)
 20    /// </summary>
 21    /// <param name="nums"></param>
 22    /// <param name="k"></param>
 23    /// <returns></returns>
 24    public int MinimumSubarrayLength(int[] nums, int k)
 325    {
 326        var minLength = int.MaxValue;
 327        var windowStart = 0;
 328        var windowEnd = 0;
 329        var bitCounts = new int[32];
 30
 1131        while (windowEnd < nums.Length)
 832        {
 833            UpdateBitCounts(bitCounts, nums[windowEnd], 1);
 34
 1435            while (windowStart <= windowEnd && ConvertBitCountsToNumber(bitCounts) >= k)
 636            {
 637                minLength = Math.Min(minLength, windowEnd - windowStart + 1);
 38
 639                UpdateBitCounts(bitCounts, nums[windowStart], -1);
 40
 641                windowStart++;
 642            }
 43
 844            windowEnd++;
 845        }
 46
 347        return minLength == int.MaxValue ? -1 : minLength;
 348    }
 49
 50    private static void UpdateBitCounts(int[] bitCounts, int number, int delta)
 1451    {
 92452        for (var bitPosition = 0; bitPosition < 32; bitPosition++)
 44853        {
 44854            if (((number >> bitPosition) & 1) != 0)
 1655            {
 1656                bitCounts[bitPosition] += delta;
 1657            }
 44858        }
 1459    }
 60
 61    private static int ConvertBitCountsToNumber(int[] bitCounts)
 1062    {
 1063        var result = 0;
 64
 66065        for (var bitPosition = 0; bitPosition < 32; bitPosition++)
 32066        {
 32067            if (bitCounts[bitPosition] != 0)
 1668            {
 1669                result |= 1 << bitPosition;
 1670            }
 32071        }
 72
 1073        return result;
 1074    }
 75}