< Summary

Information
Class: LeetCode.Algorithms.SubarraysWithKDifferentIntegers.SubarraysWithKDifferentIntegersSlidingWindow
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\SubarraysWithKDifferentIntegers\SubarraysWithKDifferentIntegersSlidingWindow.cs
Line coverage
100%
Covered lines: 26
Uncovered lines: 0
Coverable lines: 26
Total lines: 61
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
SubarraysWithKDistinct(...)100%11100%
AtMostKDistinct(...)100%88100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\SubarraysWithKDifferentIntegers\SubarraysWithKDifferentIntegersSlidingWindow.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.SubarraysWithKDifferentIntegers;
 13
 14/// <inheritdoc />
 15public class SubarraysWithKDifferentIntegersSlidingWindow : ISubarraysWithKDifferentIntegers
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n)
 19    ///     Space complexity - O(n)
 20    /// </summary>
 21    /// <param name="nums"></param>
 22    /// <param name="k"></param>
 23    /// <returns></returns>
 24    public int SubarraysWithKDistinct(int[] nums, int k)
 225    {
 226        return AtMostKDistinct(nums, k) - AtMostKDistinct(nums, k - 1);
 227    }
 28
 29    private static int AtMostKDistinct(IReadOnlyList<int> nums, int k)
 430    {
 431        var result = 0;
 32
 433        var left = 0;
 34
 435        var numDictionary = new Dictionary<int, int>();
 36
 4837        for (var right = 0; right < nums.Count; right++)
 2038        {
 2039            if (!numDictionary.TryAdd(nums[right], 1))
 440            {
 441                numDictionary[nums[right]]++;
 442            }
 43
 3244            while (numDictionary.Count > k)
 1245            {
 1246                numDictionary[nums[left]]--;
 47
 1248                if (numDictionary[nums[left]] == 0)
 849                {
 850                    numDictionary.Remove(nums[left]);
 851                }
 52
 1253                left++;
 1254            }
 55
 2056            result += right - left + 1;
 2057        }
 58
 459        return result;
 460    }
 61}