题目如下:

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: 10

Example 2:

Input: 2
Output: 20

Example 3:

Input: 3
Output: 46

Note:

  • 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]

【leetcode】935. Knight Dialer的更多相关文章

  1. 【LeetCode】935. Knight Dialer 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划TLE 空间换时间,利用对称性 优化空间复杂 ...

  2. 【leetcode】688. Knight Probability in Chessboard

    题目如下: On an NxN chessboard, a knight starts at the r-th row and c-th column and attempts to make exa ...

  3. 【LeetCode】688. Knight Probability in Chessboard 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/knight-pr ...

  4. 【LeetCode】代码模板,刷题必会

    目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...

  5. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  6. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  7. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  8. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  9. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

随机推荐

  1. C#基础提升系列——C#异步编程

    C#异步编程 关于异步的概述,这里引用MSDN的一段文字: 异步编程是一项关键技术,使得能够简单处理多个核心上的阻塞 I/O 和并发操作. 如果需要 I/O 绑定(例如从网络请求数据或访问数据库),则 ...

  2. C# json对象中包含数组对象时,如何存入数据库

    前端创建的的对象例如: C#端这样将数组提取出来存入

  3. python不能运行

    运行python文件出现,报错please select a valid interpreter 这是因为没有选择interpreter  就是更改目录时需要重新选择pytho解析器 解决方法如下 更 ...

  4. DOS基础使用专题(强烈推荐)2

    DOS下硬件设备的使用与设置 由于电脑的普及和应用的日益深入,为了满足人们的需要,电脑的功能随着它的发展变得越来越强大,硬件设备也越来越多,如从原来的ISA及PCI声卡.调制解调器等到现在的USB硬盘 ...

  5. centos7下安装storm步骤

      前言 真是后知后觉,最近忙也要学习,把以前丢的都要拾起来.原理懂不懂也把环境搭起来学习.   环境  centos7 jdk 1.8 zookeeper 3.4.13 storm 1.2.2 安装 ...

  6. paper 167:GPU的使用Theano之tutorial

    Theano之使用GPU 英文版本:http://deeplearning.net/software/theano/tutorial/using_gpu.html          using the ...

  7. [CSP-S模拟测试]:平均数(二分答案+归并排序)

    题目描述 有一天,小$A$得到了一个长度为$n$的序列.他把这个序列的所有连续子序列都列了出来,并对每一个子序列都求了其平均值,然后他把这些平均值写在纸上,并对它们进行排序,最后他报出了第$k$小的平 ...

  8. vue数据渲染、条件判断及列表循环

    1.数据渲染  {{msg}} <template> <div id="app"> {{msg}} </div> </template&g ...

  9. ArcMap如何撤销配准

    ArcMap地理配准时,更新地理配准后,就没法撤销了. 如何解决呢,更新地理配准后,会在源文件夹中自动生成配准文件(文件格式为.over  .jgwx  .xml),可以通过删除这些文件来清除配准.

  10. 116、TensorFlow变量的版本

    import tensorflow as tf v = tf.get_variable("v", shape=(), initializer=tf.zeros_initialize ...