#440

K-th Smallest in Lexicographical Order

candidate master · 1535 · lc hard +32 · verified · 46.2% accepted · 1,647 likes · top 30%

play →

Description

Given integers n and k, imagine all integers from 1 to n sorted in lexicographic (dictionary) order. Return the element at position k in that sorted sequence (1-indexed).

Example 1:

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

Example 2:

Input: n = 1, k = 1
Output: 1

Code

1
2
3