< Summary

Information
Class: LeetCode.Algorithms.CountTheNumberOfGoodSubarrays.CountTheNumberOfGoodSubarraysTwoPointers
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\CountTheNumberOfGoodSubarrays\CountTheNumberOfGoodSubarraysTwoPointers.cs
Line coverage
100%
Covered lines: 22
Uncovered lines: 0
Coverable lines: 22
Total lines: 49
Line coverage: 100%
Branch coverage
100%
Covered branches: 8
Total branches: 8
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
CountGood(...)100%88100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\CountTheNumberOfGoodSubarrays\CountTheNumberOfGoodSubarraysTwoPointers.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.CountTheNumberOfGoodSubarrays;
 13
 14/// <inheritdoc />
 15public class CountTheNumberOfGoodSubarraysTwoPointers : ICountTheNumberOfGoodSubarrays
 16{
 17    public long CountGood(int[] nums, int k)
 218    {
 219        long result = 0;
 20
 221        var frequencyDictionary = new Dictionary<int, int>();
 22
 223        var pairsCount = 0;
 224        var left = 0;
 25
 2826        for (var right = 0; right < nums.Length; right++)
 1227        {
 1228            if (!frequencyDictionary.TryAdd(nums[right], 1))
 729            {
 730                pairsCount += frequencyDictionary[nums[right]];
 31
 732                frequencyDictionary[nums[right]]++;
 733            }
 34
 1635            while (pairsCount >= k && left <= right)
 436            {
 437                result += nums.Length - right;
 38
 439                frequencyDictionary[nums[left]]--;
 40
 441                pairsCount -= frequencyDictionary[nums[left]];
 42
 443                left++;
 444            }
 1245        }
 46
 247        return result;
 248    }
 49}