< Summary

Information
Class: LeetCode.Algorithms.CountSubarraysWithFixedBounds.CountSubarraysWithFixedBoundsSlidingWindow
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\CountSubarraysWithFixedBounds\CountSubarraysWithFixedBoundsSlidingWindow.cs
Line coverage
100%
Covered lines: 26
Uncovered lines: 0
Coverable lines: 26
Total lines: 59
Line coverage: 100%
Branch coverage
85%
Covered branches: 12
Total branches: 14
Branch coverage: 85.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
CountSubarrays(...)85.71%1414100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\CountSubarraysWithFixedBounds\CountSubarraysWithFixedBoundsSlidingWindow.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.CountSubarraysWithFixedBounds;
 13
 14/// <inheritdoc />
 15public class CountSubarraysWithFixedBoundsSlidingWindow : ICountSubarraysWithFixedBounds
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n)
 19    ///     Space complexity - O(1)
 20    /// </summary>
 21    /// <param name="nums"></param>
 22    /// <param name="minK"></param>
 23    /// <param name="maxK"></param>
 24    /// <returns></returns>
 25    public long CountSubarrays(int[] nums, int minK, int maxK)
 226    {
 227        long count = 0;
 28
 229        var lastMinKPosition = -1;
 230        var lastMaxKPosition = -1;
 31
 232        var left = 0;
 33
 2434        for (var right = 0; right < nums.Length; right++)
 1035        {
 1036            if (nums[right] == minK)
 537            {
 538                lastMinKPosition = right;
 539            }
 40
 1041            if (nums[right] == maxK)
 642            {
 643                lastMaxKPosition = right;
 644            }
 45
 1046            if (nums[right] < minK || nums[right] > maxK)
 147            {
 148                left = right + 1;
 149            }
 50
 1051            if (lastMinKPosition != -1 && lastMaxKPosition != -1)
 852            {
 853                count += Math.Max(0, Math.Min(lastMinKPosition, lastMaxKPosition) - left + 1);
 854            }
 1055        }
 56
 257        return count;
 258    }
 59}