Algorithm
Problem Name: 440. K-th Smallest in Lexicographical Order
Given two integers n
and k
, return the kth
lexicographically smallest integer in the range [1, n]
.
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
Constraints:
1 <= k <= n <= 109
Code Examples
#1 Code Example with Javascript Programming
Code -
Javascript Programming
const findKthNumber = function(n, k) {
let curr = 1
k = k - 1
while (k > 0) {
let steps = calSteps(n, curr, curr + 1)
if (steps <= k) {
curr++
k -= steps
} else {
curr *= 10
k--
}
}
return curr
}
function calSteps(n, n1, n2) {
let steps = 0
while (n1 <= n) {
steps += Math.min(n + 1, n2) - n1
n1 *= 10
n2 *= 10
}
return steps
}
Copy The Code &
Try With Live Editor
Input
n = 13, k = 2
Output
10