LeetCode 题解工作台

破解保险箱

有一个需要密码才能打开的保险箱。密码是 n 位数, 密码的每一位都是范围 [0, k - 1] 中的一个数字。 保险箱有一种特殊的密码校验方法,你可以随意输入密码序列,保险箱会自动记住 最后 n 位输入 ,如果匹配,则能够打开保险箱。 例如,正确的密码是 "345" ,并且你输入的是 "012345…

category

3

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

困难 · 图·DFS·traversal

bolt

答案摘要

我们可以对题目中所描述的内容构建有向图:将每个点看作一个长度为 的 字符串,每条边都带有一个从 到 的字符。如果点 到点 之间有一条有向边 ,假设 携带的字符为 ,那么 的末尾 个字符形成的字符串等于 ,此时边 就表示了一个长度为 的密码。 在这个有向图中,一共有 个点,每个点都有 条出边,也有 条入边,因此,该有向图存在欧拉回路,欧拉回路所经过的路径拼接起来就是题目中…

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 图·DFS·traversal 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

有一个需要密码才能打开的保险箱。密码是 n 位数, 密码的每一位都是范围 [0, k - 1] 中的一个数字。

保险箱有一种特殊的密码校验方法,你可以随意输入密码序列,保险箱会自动记住 最后 n 位输入 ,如果匹配,则能够打开保险箱。

  • 例如,正确的密码是 "345" ,并且你输入的是 "012345"
    • 输入 0 之后,最后 3 位输入是 "0" ,不正确。
    • 输入 1 之后,最后 3 位输入是 "01" ,不正确。
    • 输入 2 之后,最后 3 位输入是 "012" ,不正确。
    • 输入 3 之后,最后 3 位输入是 "123" ,不正确。
    • 输入 4 之后,最后 3 位输入是 "234" ,不正确。
    • 输入 5 之后,最后 3 位输入是 "345" ,正确,打开保险箱。

在只知道密码位数 n 和范围边界 k 的前提下,请你找出并返回确保在输入的 某个时刻 能够打开保险箱的任一 最短 密码序列 。

 

示例 1:

输入:n = 1, k = 2
输出:"10"
解释:密码只有 1 位,所以输入每一位就可以。"01" 也能够确保打开保险箱。

示例 2:

输入:n = 2, k = 2
输出:"01100"
解释:对于每种可能的密码:
- "00" 从第 4 位开始输入。
- "01" 从第 1 位开始输入。
- "10" 从第 3 位开始输入。
- "11" 从第 2 位开始输入。
因此 "01100" 可以确保打开保险箱。"01100"、"10011" 和 "11001" 也可以确保打开保险箱。

 

提示:

  • 1 <= n <= 4
  • 1 <= k <= 10
  • 1 <= kn <= 4096
lightbulb

解题思路

方法一:欧拉回路

我们可以对题目中所描述的内容构建有向图:将每个点看作一个长度为 n1n-1kk 字符串,每条边都带有一个从 00k1k-1 的字符。如果点 uu 到点 vv 之间有一条有向边 ee,假设 ee 携带的字符为 cc,那么 u+cu+c 的末尾 k1k-1 个字符形成的字符串等于 vv,此时边 u+cu+c 就表示了一个长度为 nn 的密码。

在这个有向图中,一共有 kn1k^{n-1} 个点,每个点都有 kk 条出边,也有 kk 条入边,因此,该有向图存在欧拉回路,欧拉回路所经过的路径拼接起来就是题目中的答案。

时间复杂度 O(kn)O(k^n),空间复杂度 O(kn)O(k^n)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution:
    def crackSafe(self, n: int, k: int) -> str:
        def dfs(u):
            for x in range(k):
                e = u * 10 + x
                if e not in vis:
                    vis.add(e)
                    v = e % mod
                    dfs(v)
                    ans.append(str(x))

        mod = 10 ** (n - 1)
        vis = set()
        ans = []
        dfs(0)
        ans.append("0" * (n - 1))
        return "".join(ans)
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    The candidate's understanding of Eulerian circuits and graph traversal will be essential for solving this problem.

  • question_mark

    Look for familiarity with depth-first search and backtracking techniques as key concepts for solving this efficiently.

  • question_mark

    The ability to break down the problem into smaller graph-related subproblems and use traversal optimizations will be a critical signal of readiness.

warning

常见陷阱

外企场景
  • error

    Failing to recognize the problem as a graph traversal one, leading to inefficient brute force solutions.

  • error

    Not considering the constraints properly, which may result in unnecessary revisits to password combinations.

  • error

    Overlooking the Eulerian circuit approach, which could lead to overly complex or incorrect solutions.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Allowing a different number of digits, which could alter the traversal space.

  • arrow_right_alt

    Modifying the range of possible digits (k), potentially requiring a new approach to handle larger spaces.

  • arrow_right_alt

    Changing the sequence length (n), making the problem easier or harder depending on the value of n.

help

常见问题

外企场景

破解保险箱题解:图·DFS·traversal | LeetCode #753 困难