【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-1hops. 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
Ndigits 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-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
随机推荐
- Web核心之最简单最简单最简单的登录页面
需求分析: 在登录页面提交用户名和密码 在Servlet中接收提交的参数,封装为User对象,然后调用DAO中的方法进行登录验证 在DAO中进行数据库查询操作,根据参数判断是否有对象的用户存在 在Se ...
- mysql delete from left 多表条件删除
DELETE n from news n LEFT JOIN news2 d ON n.id=d.id WHERE d.id IS null
- Struts2基础-3 -继承ActionSupport接口创建Action控制器+javaBean接收请求参数+ 默认Action配置处理请求错误 + 使用ActionContext访问ServletAPI
1.目录结构及导入的jar包 2.web.xml 配置 <?xml version="1.0" encoding="UTF-8"?> <web ...
- XenServer(服务器虚拟化平台)
Citrix Xenserver,思杰基于Xen的虚拟化服务器.Citrix XenServer是一种全面而易于管理的服务器虚拟化平台,基于强大的 Xen Hypervisor 程序之上.Xen技术被 ...
- python中常用得字符串,列表函数汇总
字符串函数: 1,replace函数,替换函数.s = s.replace(old,new),老得元素被新的元素替换.注意不能直接写s.replace(old,new).要写s=s.replace(o ...
- Docker容器数据卷-Volume详解
Docker中的数据可以存储在类似于虚拟机磁盘的介质中,在Docker中称为数据卷(Data Volume).数据卷可以用来存储Docker应用的数据,也可以用来在Docker容器间进行数据共享.数据 ...
- sdb报告-10 错误问题定位
# sdb报告-10 错误问题定位在sdb 的集群环境中,如果面对的是一个高并发的操作场景,有时候会莫名其妙地报告 -10 错误. 在 sdb 的错误列表中,-10 错误代表:系统错误. 这是一个笼统 ...
- Ubuntu安装 docker
安装docker首先要需要一台宿主机, 我目前用VMvare下安装的Ubuntu16.04系统为宿主机,进行docker安装测试. ubuntu安装时选的中文环境,生成的sources.list里面的 ...
- 面试题:Nginx负载均衡的算法怎么实现的?为什么要做动静分离?
面试题 Nginx负载均衡的算法怎么实现的?Nginx 有哪些负载均衡策略?Nginx为什么要做动静分离? 面试官心理剖析 主要是看应聘人员对Nginx的基本原理是否熟悉,需要应聘人员能够根据实际业务 ...
- intellij中maven不能导入pom文件中指定的jar包
pom文件配置依赖的jar包版本,可以有默认的版本,如下 <profiles> <profile> <id>default_version</id> & ...
. 