Problem Description
Given two integers n and k, return the kth lexicographically smallest integer in the range [1, n]. For instance, when n = 13 and k = 2, the lexicographical order of numbers is [1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9], so the answer is 10.
Key Insights
- Visualize the set [1, n] as being organized in a trie-like structure rooted by their prefixes.
- Instead of sorting or enumerating all numbers, count how many nodes (numbers) exist under a given prefix.
- Use counting to decide if the kth number lies within the current prefix subtree or if we need to skip to the next sibling.
- A helper method computes the number of nodes within a prefix interval while respecting the upper bound n.
Space and Time Complexity
Time Complexity: Approximately O(log(n) * log(n)). At each step, the counting process runs in O(log(n)) and there are roughly O(log(n)) steps. Space Complexity: O(1) extra space.
Solution
We traverse the lexicographical tree without constructing its full structure. Start with the first number, 1, then repeatedly count the number of numbers (steps) under the current prefix. If the number of steps under the prefix is less than or equal to k, it means the kth number is not in this subtree, so we skip it by moving to the next prefix. Otherwise, we dive into the subtree by appending a digit (multiplying the prefix by 10) and decrement k accordingly. This approach efficiently finds the kth lexicographical number.