【LeetCode】753. Cracking the Safe 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址: https://leetcode.com/problems/cracking-the-safe/description/
题目描述:
There is a box protected by a password. The password is n digits, where each letter can be one of the first k digits 0, 1, ..., k-1.
You can keep inputting the password, the password will automatically be matched against the last n digits entered.
For example, assuming the password is "345", I can open it when I type "012345", but I enter a total of 6 digits.
Please return any string of minimum length that is guaranteed to open the box after the entire string is inputted.
Example 1:
Input: n = 1, k = 2
Output: "01"
Note: "10" will be accepted too.
Example 2:
Input: n = 2, k = 2
Output: "00110"
Note: "01100", "10011", "11001" will be accepted too.
Note:
- n will be in the range [1, 4].
- k will be in the range [1, 10].
- k^n will be at most 4096.
题目大意
题目描述是一个智能门锁,这个门锁只识别最近的n个字符,只要有连续的n个字符输入对了,前面无论输入什么都无所谓。那么,让我们求一个最短的万能钥匙串,能破解这个门锁的所有可能密码。
解题方法
显而易见,如果要这个万能钥匙串足够短,那么可以预料的是,每个密码都复用前面的密码,那么最优情况下就是,每个密码复用前面的n - 1位密码。额,这个数学问题叫做De Bruijin sequence,已经证明了每个可能的密码都可以在一个万能钥匙串中出现并且只出现一次。(Leetcode出这样的题也是让人没辙)
n位的密码锁,每个位置可以有k个数,所以总的可能性是k ^ n个。然后我们从前n位是0的状态开始,每次在后面添加一个新的字符,同时使用set保存这个出现过的新密码。当新密码的种类数等于 k ^ n时,搜索截止。
数学上已经郑明,这样的串是最短的串,而且,能解决所有可能的密码数。
python由于是值传递,可以通过给函数传list的方式,直接修改list内会导致外边的List也变化,但是传string不行。string是不可变的对象,函数内部修改string不会影响到外边。因此,如果需要动态生成字符串,可以把string变成list当做函数的参数。
时间复杂度是O(Nlog(N)),空间复杂度是O(N)。
class Solution(object):
def crackSafe(self, n, k):
"""
:type n: int
:type k: int
:rtype: str
"""
res = ["0"] * n
size = k ** n
visited = set()
visited.add("".join(res))
if self.dfs(res, visited, size, n, k):
return "".join(res)
return ""
def dfs(self, res, visited, size, n, k):
if len(visited) == size:
return True
node = "".join(res[len(res) - n + 1:])
for i in range(k):
node = node + str(i)
if node not in visited:
res.append(str(i))
visited.add(node)
if self.dfs(res, visited, size, n, k):
return True
res.pop()
visited.remove(node)
node = node[:-1]
参考资料:
http://www.cnblogs.com/grandyang/p/8452361.html
https://www.youtube.com/watch?v=kRdlLahVZDc
日期
2018 年 10 月 5 日 —— 转眼假期要结束了!!
【LeetCode】753. Cracking the Safe 解题报告(Python)的更多相关文章
- [LeetCode] 753. Cracking the Safe 破解密码
There is a box protected by a password. The password is n digits, where each letter can be one of th ...
- 【LeetCode】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- 【LeetCode】649. Dota2 Senate 解题报告(Python)
[LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 【LeetCode】911. Online Election 解题报告(Python)
[LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
- 【LeetCode】886. Possible Bipartition 解题报告(Python)
[LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
- 【LeetCode】870. Advantage Shuffle 解题报告(Python)
[LeetCode]870. Advantage Shuffle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...
- 【LeetCode】593. Valid Square 解题报告(Python)
[LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
随机推荐
- R语言与医学统计图形-【16】ggplot2几何对象之标签与文本
ggplot2绘图系统--添加标签与文本.数学表达式.条形图文本.注释 1. 文本与标签添加 geom_label的文本将以标签形式出现,即文本会带有一个背景色. geom_text则是纯文本形式展示 ...
- 深度探讨 PHP 之性能
1.缘起 关于PHP,很多人的直观感觉是PHP是一种灵活的脚本语言,库类丰富,使用简单,安全,非常适合WEB开发,但性能低下.PHP的性能是否真的就 如同大家的感觉一样的差呢?本文就是围绕这么一个话题 ...
- POLLOUT/POLLINT事件触发测试
一般poll使用过程中都是检测POLLIN事件,表示描述符有事件可以处理了,需要我们处理.对POLLOUT事件触发的方式相对较少,网上也有很多对此触发的疑问,利用实际项目中用的一个用法,下面做了个测试 ...
- 【风控算法】一、变量分箱、WOE和IV值计算
一.变量分箱 变量分箱常见于逻辑回归评分卡的制作中,在入模前,需要对原始变量值通过分箱映射成woe值.举例来说,如"年龄"这一变量,我们需要找到合适的切分点,将连续的年龄打散到不同 ...
- Linux磁盘分区(二)之挂载卸载常用命令
Linux磁盘分区(二)之挂载卸载常用命令 转自:https://blog.csdn.net/qq_36183935/article/details/81053383 https: ...
- Git初始化及仓库创建和操作
一.基本信息配置 1.全局配置用户名 git config --global user.name "YeHuan-byte" 2.全局配置邮箱 git config --globa ...
- 【科研工具】流程图软件Visio Pro 2019 详细安装破解教程
[更新区] 安装教程我下周会在bilibili上传视频,这周事情太多暂时先不弄. [注意] 安装Visio需要和自己的Word版本一样,这里因为我的Word是学校的正版2019(所以学校为什么正版没买 ...
- Windows内存管理-分段
0x01原因 分段的产生原属于安全问题. 一个程序可以自由的访问不属于它的内存位置,甚至可以对那些内容进行修改.这也导致安全问题 促使一种内存隔离的手段 分段的产生. 0x02分段原理 处理器要求在加 ...
- vm16虚拟机安装win11
vm16虚拟机安装win11 参考https://baijiahao.baidu.com/s?id=1712702900207158969&wfr=spider&for=pc win1 ...
- Tableau如何绘制双柱折线组合图2
一.数据源准备 二.创建计算字段月度-拖拽至列-右键-精确日期 CASE[指标] WHEN "同期"then DATETRUNC('day',[日期])-5 WHEN " ...