The Indian Engineer

Problem 440 K-th Smallest in Lexicographical Order

Posted on 6 mins

Trie

Problem Statement

Link - Problem 440

Question

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:

Solution

class Solution {
public:
    int findKthNumber(int n, int k) {
        int curr = 1;
        k--;

        while (k > 0) {
            int step = countSteps(n, curr, curr + 1);
            if (step <= k) {
                curr++;
                k -= step;
            } else {
                curr *= 10;
                k--;
            }
        }

        return curr;
    }

private:
    int countSteps(int n, long prefix1, long prefix2) {
        int steps = 0;
        while (prefix1 <= n) {
            steps += min((long)(n + 1), prefix2) - prefix1;
            prefix1 *= 10;
            prefix2 *= 10;
        }
        return steps;
    }
};

Complexity Analysis

| Algorithm                              | Time Complexity  | Space Complexity |
| -------------------------------------- | ---------------- | ---------------- |
| Incremental Lexicographical Generation | O(log(n) log(k)) | O(1)             |

Explanation

Intial Thoughts

To approach this problem, we need to consider the lexicographical order of the numbers in the given range. This involves comparing numbers based on their string representations rather than their actual numerical values. Initially, we should think about how the order changes as we move from one number to the next, and identify any patterns or regularities. We should also consider how the range of numbers affects the order. Key points to consider are: understanding lexicographical order, recognizing patterns in the order, identifying key milestones like powers of 10, and considering the impact of the range on the order.

Intuitive Analysis

The key to solving this problem intuitively lies in understanding how numbers are arranged in lexicographical order and recognizing the pattern that emerges when counting through the numbers. Intuitive analysis involves realizing that as we move from one number to the next, the order can change significantly at certain points, such as when moving from single-digit to two-digit numbers. We should think about how many steps it takes to move through a certain range of numbers in lexicographical order, such as the difference between consecutive numbers and how this changes as numbers get larger. Key points for intuitive analysis include: stepping through the numbers in lexicographical order, recognizing how the number of steps changes as numbers increase, understanding the role of powers of 10 as dividers between large groups of numbers, and considering how to navigate through the numbers efficiently to find the kth number.

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Footnote

This question is rated as Hard difficulty.


Similar Questions:

Title URL Difficulty
Count Special Integers https://leetcode.com/problems/count-special-integers Hard