You are given two integers n and k, return the kth lexicographically smallest integer in the range [1, n].
Example 1:
Input: n = 10, k = 3
Output: 2Explanation: The lexicographical order is [1,10,2,3,4,5,6,7,8,9]. The third smallest number is 2.
Example 2:
Input: n = 11, k = 3
Output: 2Explanation: The lexicographical order is [1,10,11,2,3,4,5,6,7,8,9]. The third smallest number is 11.
Constraints:
1 <= k <= n <= 1,000,000,000The simplest approach converts all numbers from 1 to n into strings and sorts them lexicographically. In lexicographical order, "10" comes before "2" because '1' < '2'. After sorting, we simply return the k-th element. This is straightforward but inefficient for large n.
1 to n as strings.k-th string converted back to an integer.Numbers in lexicographical order form a tree structure where each prefix leads to its extensions. For example, prefix "1" leads to "10", "11", ..., "19", "100", etc. We can count how many numbers exist under any prefix without enumerating them. Starting at "1", we count how many numbers lie in the subtree rooted at the current prefix. If this count is less than or equal to the remaining k, we skip this entire subtree and move to the next sibling. Otherwise, we descend into the subtree by appending a digit.
count(cur) to calculate how many numbers in [1, n] have cur as a prefix.cur = 1 and i = 1 (position in lexicographical order).i < k:steps = count(cur).i + steps <= k, move to the next sibling: cur++ and i += steps.cur *= 10 and i++.cur when i == k.class Solution:
def findKthNumber(self, n: int, k: int) -> int:
cur = 1
i = 1
def count(cur):
res = 0
nei = cur + 1
while cur <= n:
res += min(nei, n + 1) - cur
cur *= 10
nei *= 10
return res
while i < k:
steps = count(cur)
if i + steps <= k:
cur = cur + 1
i += steps
else:
cur *= 10
i += 1
return curIn lexicographical order, "10" comes before "2" because strings are compared character by character. Treating numbers numerically instead of as strings leads to completely wrong results. The sequence starts 1, 10, 100, ..., 2, 20, ....
When counting numbers under a prefix, the prefix and its neighbor are multiplied by 10 repeatedly. For large n, these values can exceed 32-bit integer limits. Use 64-bit integers (long in Java, long long in C++) for the counting logic.
The count function must include the prefix itself plus all its extensions up to n. The formula min(neighbor, n + 1) - cur counts numbers in the current level. A common error is using n instead of n + 1, which excludes the boundary value.
The position variable i starts at 1 (representing we are at number 1). When moving to a child, increment i by 1. When skipping a subtree, increment i by the step count. Confusing these increments places you at the wrong number.
When descending from prefix "1" to "10", you multiply by 10. But if 10 > n, you cannot descend further. The algorithm must handle cases where the current prefix exceeds n after multiplication, which the count function naturally handles by returning 0.