题目如下:

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. centos6安装完成之后必要的配置

    centos6安装完成之后必要的配置 一配置yum源 [root@centos61 ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirro ...

  2. spring+springMVC+mybatis框架整合——配置文件说明

    如下图 web.xml配置说明: spring配置文件说明-1: spring配置文件说明-2: spring配置助记:  扫注(base) 读配(loc) 数据源(和comb(使用c3p0数据源)) ...

  3. window环境下pipd的安装

    参照:https://blog.csdn.net/jin80506/article/details/83111848 如果你还是无法使用尝试查看是否自己已经将:C:\software\Python\P ...

  4. js常用方法和检查是否有特殊字符串和倒序截取字符串

     js常用方法demo <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ ...

  5. 2018-2019-2 20175307实验三《敏捷开发与XP实践》实验报告

    实验三 敏捷开发与XP实践-1 1.仔细学习了http://www.cnblogs.com/rocedu/p/4795776.html,发布了一篇关于Google的Java编码的博客,具体内容就不在这 ...

  6. 20175126《Java程序设计》第十周学习总结

    # 20175126 2016-2017-2 <Java程序设计>第十周学习总结 ## 教材学习内容总结 - 本周学习方式主要为手动敲代码并理解内容学习. -本周学习十二章,主要内容如下: ...

  7. codeforces 559D Randomizer

    题意简述: 在一个格点图中 给定一个凸$n$边形(每个定点均在格点上),随机选择其中一些点构成一个子多边形, 求子多边形的内部点个数的期望. ----------------------------- ...

  8. java知识点拾遗:)

    一篇有用的java基础知识总结http://www.cnblogs.com/xuwujing/p/8638329.html 枚举:http://blog.csdn.net/qq_27093465/ar ...

  9. Nginx+Tomcat Session 无效问题

    omcat 和 Nginx 是相互独立的,在创建 Session 的时候,会根据部署的 Path 作为 Session Cookie 的 Path 路径,原则就是解决 Session Path 路径问 ...

  10. 常用命令--find

    语法 find path -option [ -print ] [-exec -ok command ] {} \; find . -maxdepth 1 -type f -exec mv {} /t ...