LeetCode 题解工作台

字典序的第K小数字

给定整数 n 和 k ,返回 [1, n] 中字典序第 k 小的数字。 示例 1: 输入: n = 13, k = 2 输出: 10 解释: 字典序的排列是 [1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9],所以第二小的数字是 10。 示例 2: 输入: n =…

category

1

题型

code_blocks

6

代码语言

hub

3

相关题

当前训练重点

困难 · 字典树·driven·solution·strategy

bolt

答案摘要

本题要求在区间 $[1, n]$ 中,按字典序排序后,找到第 小的数字。由于 的范围非常大(最多可达 ),我们无法直接枚举所有数字后排序。因此我们采用贪心 + 字典树模拟的策略。 我们将 $[1, n]$ 看作一棵 十叉字典树(Trie):

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 字典树·driven·solution·strategy 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给定整数 n 和 k,返回  [1, n] 中字典序第 k 小的数字。

 

示例 1:

输入: n = 13, k = 2
输出: 10
解释: 字典序的排列是 [1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9],所以第二小的数字是 10。

示例 2:

输入: n = 1, k = 1
输出: 1

 

提示:

  • 1 <= k <= n <= 109
lightbulb

解题思路

方法一:字典树计数 + 贪心构造

本题要求在区间 [1,n][1, n] 中,按字典序排序后,找到第 kk 小的数字。由于 nn 的范围非常大(最多可达 10910^9),我们无法直接枚举所有数字后排序。因此我们采用贪心 + 字典树模拟的策略。

我们将 [1,n][1, n] 看作一棵 十叉字典树(Trie)

  • 每个节点是一个前缀,根节点为空串;
  • 节点的子节点是当前前缀拼接上 090 \sim 9
  • 例如前缀 11 会有子节点 10,11,,1910, 11, \ldots, 19,而 1010 会有 100,101,,109100, 101, \ldots, 109
  • 这种结构天然符合字典序遍历。
根
├── 1
│   ├── 10
│   ├── 11
│   ├── ...
├── 2
├── ...

我们使用变量 curr\textit{curr} 表示当前前缀,初始为 11。每次我们尝试向下扩展前缀,直到找到第 kk 小的数字。

每次我们计算当前前缀下有多少个合法数字(即以 curr\textit{curr} 为前缀、且不超过 nn 的整数个数),记作 count(curr)\textit{count}(\text{curr})

  • 如果 kcount(curr)k \ge \text{count}(\text{curr}):说明目标不在这棵子树中,跳过整棵子树,前缀右移:currcurr+1\textit{curr} \leftarrow \text{curr} + 1,并更新 kkcount(curr)k \leftarrow k - \text{count}(\text{curr})
  • 否则:说明目标在当前前缀的子树中,进入下一层:currcurr×10\textit{curr} \leftarrow \text{curr} \times 10,并消耗一个前缀:kk1k \leftarrow k - 1

每一层我们将当前区间扩大 1010 倍,向下延伸到更长的前缀,直到超出 nn

时间复杂度 O(log2n)O(\log^2 n),空间复杂度 O(1)O(1)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution:
    def findKthNumber(self, n: int, k: int) -> int:
        def count(curr):
            next, cnt = curr + 1, 0
            while curr <= n:
                cnt += min(n - curr + 1, next - curr)
                next, curr = next * 10, curr * 10
            return cnt

        curr = 1
        k -= 1
        while k:
            cnt = count(curr)
            if k >= cnt:
                k -= cnt
                curr += 1
            else:
                k -= 1
                curr *= 10
        return curr
speed

复杂度分析

指标
时间O(\log(n)^2)
空间O(1)
psychology

面试官常问的追问

外企场景
  • question_mark

    Can the candidate describe a method that avoids iterating through all numbers from 1 to n?

  • question_mark

    Did the candidate implement an efficient counting function that works without generating all possible numbers?

  • question_mark

    Does the candidate understand how a Trie-driven approach can optimize the search for the k-th smallest number?

warning

常见陷阱

外企场景
  • error

    Not implementing the Trie structure efficiently, leading to excessive time complexity.

  • error

    Misunderstanding the counting mechanism and generating all numbers explicitly instead of using the prefix-based counting approach.

  • error

    Failing to consider edge cases, such as when k is at the upper or lower bound of the range.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Solving the problem with a simple iterative approach (not optimized).

  • arrow_right_alt

    Optimizing the approach further to handle extremely large values of n and k by leveraging more advanced Trie techniques.

  • arrow_right_alt

    Adapting the algorithm to handle different ranges or non-integer inputs in a lexicographical order.

help

常见问题

外企场景

字典序的第K小数字题解:字典树·driven·solution·str… | LeetCode #440 困难