【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]
【leetcode】935. Knight Dialer的更多相关文章
- 【LeetCode】935. Knight Dialer 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划TLE 空间换时间,利用对称性 优化空间复杂 ...
- 【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 ...
- 【LeetCode】688. Knight Probability in Chessboard 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/knight-pr ...
- 【LeetCode】代码模板,刷题必会
目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
随机推荐
- Python构造器及析构器:__init__与__new__及__del__
__init__与__new__这两个魔法方法组成了Python类对象的构造器,在Python类实例化时,其实最先调用的不是__init__而是__new__.__new__是负责实例化对象的,而__ ...
- BZOJ 4399: 魔法少女LJJ(线段树)
传送门 解题思路 出题人真会玩..操作\(2\)线段树合并,然后每棵线段树维护元素个数和.对于\(6\)这个询问,因为乘积太大,所以要用对数.时间复杂度\(O(nlogn)\) 代码 #include ...
- js中打地鼠游戏
<!DOCTYPE html><html lang=""><head> <mata charset = "utf-8" ...
- 使用selenium+BeautifulSoup 抓取京东商城手机信息
1.准备工作: chromedriver 传送门:国内:http://npm.taobao.org/mirrors/chromedriver/ vpn: selenium BeautifulSo ...
- Python中生成器和yield语句的用法详解
Python中生成器和yield语句的用法详解 在开始课程之前,我要求学生们填写一份调查表,这个调查表反映了它们对Python中一些概念的理解情况.一些话题("if/else控制流" ...
- Java + selenium 元素定位(1)之By id/Name/ClassName
这篇文章开始,要介绍web自动化核心的内容,也是最困难的部分了,就是定位元素,并去对元素进行操作.想要对元素进行操作,第一步,也是最重要的一步,就是要找到这个元素,如果连元素都定位不到,后续什么操作都 ...
- 定制xfce4桌面==排除appfinder的綑绑
如下等同于安装 xfce4-meta,除了不安装 app-finder emerge -avq xfwm4 xfdesktop xfce4-session xfce4-settings xfce4-t ...
- f-strings格式化输出
python3.6后加入标准库的格式化输出新的写法 不区分大小写,f,F都可以 可以加入表达式 s1 = 'haha' s = f'python{s1.upper()}' l1 = ['小明', 18 ...
- python面试题之有没有一个工具可以帮助查找python的bug和进行静态的代码分析?
pycheck pylint 本文首发于python黑洞网,博客园同步更新
- 51-python基础-python3-列表-常用列表方法- index()方法
index()方法 1-可以传入一个值,如果该值存在于列表中,就返回它的下标. 实例1: 2-如果该值不在列表中,Python 就报 ValueError. 实例2: 3-如果列表中存在重复的值,就返 ...