< Summary

Information
Class: LeetCode.Algorithms.CountCompleteSubarraysInAnArray.CountCompleteSubarraysInAnArrayFrequencyArray
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\CountCompleteSubarraysInAnArray\CountCompleteSubarraysInAnArrayFrequencyArray.cs
Line coverage
100%
Covered lines: 36
Uncovered lines: 0
Coverable lines: 36
Total lines: 75
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
CountCompleteSubarrays(...)100%1212100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\CountCompleteSubarraysInAnArray\CountCompleteSubarraysInAnArrayFrequencyArray.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.CountCompleteSubarraysInAnArray;
 13
 14/// <inheritdoc />
 15public class CountCompleteSubarraysInAnArrayFrequencyArray : ICountCompleteSubarraysInAnArray
 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 int CountCompleteSubarrays(int[] nums)
 224    {
 225        var distinctCount = 0;
 226        var distinctArray = new bool[2000];
 27
 2428        foreach (var num in nums)
 929        {
 930            if (distinctArray[num - 1])
 531            {
 532                continue;
 33            }
 34
 435            distinctArray[num - 1] = true;
 36
 437            distinctCount++;
 438        }
 39
 240        var count = 0;
 41
 242        var frequencyArray = new int[2000];
 43
 244        var left = 0;
 245        var currentDistinctCount = 0;
 46
 2247        for (var right = 0; right < nums.Length; right++)
 948        {
 949            var rightValue = nums[right] - 1;
 50
 951            if (frequencyArray[rightValue] == 0)
 752            {
 753                currentDistinctCount++;
 754            }
 55
 956            frequencyArray[rightValue]++;
 57
 1558            while (currentDistinctCount == distinctCount)
 659            {
 660                count += nums.Length - right;
 61
 662                frequencyArray[nums[left] - 1]--;
 63
 664                if (frequencyArray[nums[left] - 1] == 0)
 565                {
 566                    currentDistinctCount--;
 567                }
 68
 669                left++;
 670            }
 971        }
 72
 273        return count;
 274    }
 75}