<p>Given two integers <code>n</code> and <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>lexicographically smallest integer in the range</em> <code>[1, n]</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> n = 13, k = 2
<strong>Output:</strong> 10
<strong>Explanation:</strong> The lexicographical order is [1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9], so the second smallest number is 10.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 1, k = 1
<strong>Output:</strong> 1
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= k &lt;= n &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution {
public:
    int findKthNumber(int n, int k) {
        int curr = 1;
        k--;  // We start from 1, so decrement k by 1 to handle 0-based indexing

        while (k > 0) {
            long long steps = calculateSteps(n, curr, curr + 1);
            if (steps <= k) {
                curr++;    // Move to the next prefix
                k -= steps; // Decrease k by the number of steps
            } else {
                curr *= 10; // Move to the next level (deeper in the current prefix)
                k--;        // As we move deeper, count one less k
            }
        }

        return curr;
    }

private:
    long long calculateSteps(int n, long long n1, long long n2) {
        long long steps = 0;
        while (n1 <= n) {
            steps += min((long long)n + 1, n2) - n1;
            n1 *= 10;
            n2 *= 10;
        }
        return steps;
    }
};

7400