< Summary

Information
Class: LeetCode.Algorithms.KthSmallestInLexicographicalOrder.KthSmallestInLexicographicalOrderPrefixTree
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\KthSmallestInLexicographicalOrder\KthSmallestInLexicographicalOrderPrefixTree.cs
Line coverage
100%
Covered lines: 30
Uncovered lines: 0
Coverable lines: 30
Total lines: 64
Line coverage: 100%
Branch coverage
100%
Covered branches: 6
Total branches: 6
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
FindKthNumber(...)100%44100%
GetNodesCount(...)100%22100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\KthSmallestInLexicographicalOrder\KthSmallestInLexicographicalOrderPrefixTree.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.KthSmallestInLexicographicalOrder;
 13
 14/// <inheritdoc />
 15public class KthSmallestInLexicographicalOrderPrefixTree : IKthSmallestInLexicographicalOrder
 16{
 17    /// <summary>
 18    ///     Time complexity - O(k * log n)
 19    ///     Space complexity - O(1)
 20    /// </summary>
 21    /// <param name="n"></param>
 22    /// <param name="k"></param>
 23    /// <returns></returns>
 24    public int FindKthNumber(int n, int k)
 825    {
 826        var result = 1;
 27
 828        k--;
 29
 31930        while (k > 0)
 31131        {
 31132            var nodesCount = GetNodesCount(result, n);
 33
 31134            if (nodesCount <= k)
 27235            {
 27236                k -= nodesCount;
 27237                result++;
 27238            }
 39            else
 3940            {
 3941                result *= 10;
 3942                k--;
 3943            }
 31144        }
 45
 846        return result;
 847    }
 48
 49    private static int GetNodesCount(int prefix, int n)
 31150    {
 31151        var count = 0;
 31152        long current = prefix;
 31153        long next = prefix + 1;
 54
 191055        while (current <= n)
 159956        {
 159957            count += (int)(Math.Min(n + 1, next) - current);
 159958            current *= 10;
 159959            next *= 10;
 159960        }
 61
 31162        return count;
 31163    }
 64}