< Summary

Information
Class: LeetCode.Algorithms.ContinuousSubarrays.ContinuousSubarraysTwoPointers
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ContinuousSubarrays\ContinuousSubarraysTwoPointers.cs
Line coverage
93%
Covered lines: 31
Uncovered lines: 2
Coverable lines: 33
Total lines: 73
Line coverage: 93.9%
Branch coverage
80%
Covered branches: 8
Total branches: 10
Branch coverage: 80%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
ContinuousSubarrays(...)80%101093.93%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ContinuousSubarrays\ContinuousSubarraysTwoPointers.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.ContinuousSubarrays;
 13
 14/// <inheritdoc />
 15public class ContinuousSubarraysTwoPointers : IContinuousSubarrays
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n)
 19    ///     Space complexity - O(1)
 20    /// </summary>
 21    /// <param name="nums"></param>
 22    /// <returns></returns>
 23    public long ContinuousSubarrays(int[] nums)
 224    {
 225        long result = 0;
 26
 227        var left = 0;
 228        var right = 0;
 29
 30        long windowLength;
 31
 232        var currentMin = nums[right];
 233        var currentMax = nums[right];
 34
 1835        for (right = 0; right < nums.Length; right++)
 736        {
 737            currentMin = Math.Min(currentMin, nums[right]);
 738            currentMax = Math.Max(currentMax, nums[right]);
 39
 740            if (currentMax - currentMin <= 2)
 641            {
 642                continue;
 43            }
 44
 145            windowLength = right - left;
 146            result += windowLength * (windowLength + 1) / 2;
 47
 148            left = right;
 149            currentMin = currentMax = nums[right];
 50
 251            while (left > 0 && Math.Abs(nums[right] - nums[left - 1]) <= 2)
 152            {
 153                left--;
 54
 155                currentMin = Math.Min(currentMin, nums[left]);
 156                currentMax = Math.Max(currentMax, nums[left]);
 157            }
 58
 159            if (left >= right)
 060            {
 061                continue;
 62            }
 63
 164            windowLength = right - left;
 165            result -= windowLength * (windowLength + 1) / 2;
 166        }
 67
 268        windowLength = right - left;
 269        result += windowLength * (windowLength + 1) / 2;
 70
 271        return result;
 272    }
 73}