【leetcode】935. Knight Dialer
题目如下:
A chess knight can move as indicated in the chess diagram below:
.
This time, we place our chess knight on any numbered key of a phone pad (indicated above), and the knight makes
N-1
hops. Each hop must be from one key to another numbered key.Each time it lands on a key (including the initial placement of the knight), it presses the number of that key, pressing
N
digits total.How many distinct numbers can you dial in this manner?
Since the answer may be large, output the answer modulo
10^9 + 7
.Example 1:
Input: 1
Output: 10Example 2:
Input: 2
Output: 20Example 3:
Input: 3
Output: 46Note:
1 <= N <= 5000
解题思路:很明显的动态规划的场景。首先我们可以维护如下的一个映射字典:key为到达的数字,value为可以由哪些数字经过一次跳跃到达key的数字。接下来假设dp[i][j] 为经过跳跃i次并且最后一次跳跃的终点是j,那么有dp[i][j] = dp[i-1][dic[j][0]] + dp[i-1][dic[j][1]] + ... dp[i-1][dic[j][n]]。最终的结果就是dp[N][0] + dp[N][1] + ... dp[N][9]。后来经过测试发现,这种解法会超时,因为题目约定了N最大是5000,因此可以事先计算出1~5000的所有结果缓存起来。
dic[1] = [6,8]
dic[2] = [7,9]
dic[3] = [4,8]
dic[4] = [3,9,0]
dic[5] = []
dic[6] = [1,7,0]
dic[7] = [2,6]
dic[8] = [1,3]
dic[9] = [2,4]
dic[0] = [4,6]
代码如下:
class Solution(object):
res = []
def knightDialer(self, N):
"""
:type N: int
:rtype: int
"""
if len(self.res) != 0:
return self.res[N-1]
dic = {}
dic[1] = [6,8]
dic[2] = [7,9]
dic[3] = [4,8]
dic[4] = [3,9,0]
dic[5] = []
dic[6] = [1,7,0]
dic[7] = [2,6]
dic[8] = [1,3]
dic[9] = [2,4]
dic[0] = [4,6]
dp = []
for i in range(5001):
if i == 0:
tl = [1] * 10
else:
tl = [0] * 10
dp.append(tl)
for i in range(5001):
for j in range(10):
for k in dic[j]:
dp[i][j] += dp[i-1][k]
for i in range(5001):
self.res.append(sum(dp[i]) % (pow(10,9) + 7))
return self.res[N-1]
最新文章
- C#如何测试代码运行时间
- Java以基础类库
- Java--如何使用sun.misc.Unsafe完成compareAndSwapObject原子操作
- STM8L --- External interrupt
- C#中派生类调用基类构造函数用法分析
- Linux服务和运行级别科普
- 剑指offer系列32-----对称二叉树的判断
- 【POJ】【1704】Georgia and Bob
- 【转】OS X Mavericks: 防止 Mac 进入睡眠 -- 不错
- python -i filename
- EL表达式(转)
- HTML5新特性之CSS+HTML5实例
- _foreach
- Angular19 自定义表单控件
- .NET 设计模式的六大原则理论知识
- vue2.0生命周期
- SkylineGlobe 如何二次开发实现天际线分析功能
- 关于:HTTP Header ->; Content-Type: text/plain Cache-Control: no-cache IE浏览器弹出错误下载对话
- Spring Session Redis
- centos7.5下安装teamview
热门文章
- Python的";random";函数的使用(一)
- 微信小程序中显示html富文本的方法
- mui滚动区域的实现
- IOC(控制反转)和DI(依赖注入)
- php nl2br()函数 语法
- paper 159:文章解读:From Facial Parts Responses to Face Detection: A Deep Learning Approach--2015ICCV
- BZOJ 4484: [Jsoi2015]最小表示(拓扑排序+bitset)
- LOJ 3092 「BJOI2019」排兵布阵 ——DP
- 15 个最佳 jQuery 翻书效果插件
- HTTP协议之-URL