< Summary

Information
Class: LeetCode.Algorithms.HeightChecker.HeightCheckerCountingSort
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\HeightChecker\HeightCheckerCountingSort.cs
Line coverage
100%
Covered lines: 23
Uncovered lines: 0
Coverable lines: 23
Total lines: 55
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
HeightChecker(...)100%88100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\HeightChecker\HeightCheckerCountingSort.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.HeightChecker;
 13
 14/// <inheritdoc />
 15public class HeightCheckerCountingSort : IHeightChecker
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n + k), where n is the number of elements in the input array and k is the range of the i
 19    ///     values
 20    ///     Space complexity - O(n)
 21    /// </summary>
 22    /// <param name="heights"></param>
 23    /// <returns></returns>
 24    public int HeightChecker(int[] heights)
 325    {
 326        var maxHeight = heights.Max();
 27
 328        var count = new int[maxHeight + 1];
 29
 4130        foreach (var height in heights)
 1631        {
 1632            count[height]++;
 1633        }
 34
 335        var result = 0;
 336        var currentHeight = 0;
 37
 4138        foreach (var height in heights)
 1639        {
 3040            while (count[currentHeight] == 0)
 1441            {
 1442                currentHeight++;
 1443            }
 44
 1645            if (currentHeight != height)
 846            {
 847                result++;
 848            }
 49
 1650            count[currentHeight]--;
 1651        }
 52
 353        return result;
 354    }
 55}

Methods/Properties

HeightChecker(System.Int32[])