【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: 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的更多相关文章
- 【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-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
随机推荐
- 一个简单的JSP 连接MySQL使用实例
一.软件环境 下载并安装MySQL,Tomacat,JDBC.MyEclipse或其他IDE. 二.环境配置 将其环境变量配置好之后,下载Java 专用的连接MySQL的驱动包JDBC,有人会发现在一 ...
- boost system
boost::system::error_code is the most basic class in Boost.System; it represents operating system-sp ...
- Servlet 第一天
package com.servlet; import java.io.IOException; import javax.servlet.Servlet; import javax.servlet. ...
- SQL索引优化方法
SQL索引优化方法 以下是代码片段: ROW_NUMBER() OVER(ORDER BY ResumeCreateTime DESC) as [RowID] ,[TopDegree] ,[Degre ...
- php max()函数 语法
php max()函数 语法 作用:从所有参数中找到最大数 语法:max(X,Y,Z) 或者max(array(X,Y,Z)) 参数:max函数中参数至少一个,可以多个参数,也可以是数组. 说明:如果 ...
- kafka-consumer.properties
# Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreement ...
- <三剑客> 老二:sed命令用法
sed命令的用法: sed是一种流编辑器,它是文本处理中非常中的工具,能够完美的配合正则表达式使用,功能不同凡响.处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space ...
- cannot access Input/output error
ls: cannot access Input/output errorls: cannot open directory .: Input/output error 硬盘故障,只读或只写,你可以d ...
- [CSP-S模拟测试]:序列(主席树)
题目描述 小$A$把自己之前得到的序列展示给了小$B$,不过这一次,他并不要求小$B$模仿他之前的行为.他给了小$B$一些询问,每个询问都是$l\ r\ x$的形式,要求小$B$数出在序列的第$l$个 ...
- SQL 关键字的使用顺序
1.查询中用到的关键词主要包含六个,并且他们的顺序依次为 select --> from --> where --> group by --> having --> or ...