Leetcode: K-th Smallest in Lexicographical Order

时间:2021-10-23 21:19:16
Given integers n and k, find the lexicographically k-th smallest integer in the range from 1 to n.

Note: 1 ≤ k ≤ n ≤ 109.

Example:

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.

第二遍做法:参考https://discuss.leetcode.com/topic/64624/concise-easy-to-understand-java-5ms-solution-with-explaination

Leetcode: K-th Smallest in Lexicographical Order

Actually this is a denary tree (each node has 10 children). Find the kth element is to do a k steps preorder traverse of the tree.

Initially, image you are at node 1 (variable: curr),
the goal is move (k - 1) steps to the target node x. (substract steps from k after moving)
when k is down to 0, curr will be finally at node x, there you get the result.

we don't really need to do a exact k steps preorder traverse of the denary tree, the idea is to calculate the steps between curr and curr + 1 (neighbor nodes in same level), in order to skip some unnecessary moves.

Main function
Firstly, calculate how many steps curr need to move to curr + 1.

  1. if the steps <= k, we know we can move to curr + 1, and narrow down k to k - steps.

  2. else if the steps > k, that means the curr + 1 is actually behind the target node x in the preorder path, we can't jump to curr + 1. What we have to do is to move forward only 1 step (curr * 10 is always next preorder node) and repeat the iteration.

calSteps function

    1. how to calculate the steps between curr and curr + 1?
      Here we come up a idea to calculate by level.
      Let n1 = curr, n2 = curr + 1.
      n2 is always the next right node beside n1's right most node (who shares the same ancestor "curr")
      (refer to the pic, 2 is right next to 1, 20 is right next to 19, 200 is right next to 199).

    2. so, if n2 <= n, what means n1's right most node exists, we can simply add the number of nodes from n1 to n2 to steps.

    3. else if n2 > n, what means n (the biggest node) is on the path between n1 to n2, add (n + 1 - n1) to steps.

    4. organize this flow to "steps += Math.min(n + 1, n2) - n1; n1 *= 10; n2 *= 10;"

 public class Solution {
public int findKthNumber(int n, int k) {
int curr = 1;
k--;
while (k > 0) {
int steps = calc(n, curr, curr+1);
if (k >= steps) {
k -= steps;
curr = curr + 1;
}
else {
k -= 1;
curr = curr * 10;
}
}
return curr;
} public int calc(int n, long n1, long n2) {
int steps = 0;
while (n1 <= n) {
steps += Math.min(n+1, n2) - n1;
n1 *= 10;
n2 *= 10;
}
return steps;
}
}

下面是我自己的方法,参考Lexicographical Numbers这道题,对是对的,但是挨个访问,没有skip, TLE了

 public class Solution {
public int findKthNumber(int n, int k) {
int cur = 1;
for (int i=1; i<k; i++) {
if (cur * 10 <= n) {
cur = cur * 10;
}
else {
while (cur>10 && cur%10==9) {
cur /= 10;
}
cur = cur + 1;
}
}
return cur;
}
}