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: 46
Note:
1 <= N <= 5000
Approach #1: DP. [Java]
class Solution {
public int knightDialer(int N) {
int mod = 1000000007;
int[][][] dp = new int[N+1][5][4];
for (int j = 0; j < 4; ++j)
for (int k = 0; k < 3; ++k)
dp[1][j][k] = 1;
dp[1][3][0] = dp[1][3][2] = 0;
int[][] dirs = {{1, 2}, {1, -2}, {2, 1}, {2, -1},
{-1, 2}, {-1, -2}, {-2, 1}, {-2, -1}};
for (int k = 2; k <= N; ++k) {
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 3; ++j) {
if (i == 3 && j != 1) continue;
for (int d = 0; d < 8; ++d) {
int x_ = i + dirs[d][0];
int y_ = j + dirs[d][1];
if (x_ < 0 || y_ < 0 || x_ >= 4 || y_ >= 3) continue;
dp[k][i][j] = (dp[k][i][j] + dp[k-1][x_][y_]) % mod;
}
}
}
}
int ans = 0;
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 3; ++j) {
ans = (ans + dp[N][i][j]) % mod;
// System.out.print(dp[N][i][j] + " ");
}
// System.out.println("ans = " + ans);
}
return ans;
}
}
Analysis:
We can define dp[k][i][j] as of ways to dial and the last key is (i, j) after k steps
Note: dp[*][3][0], dp[*][3][2] are always zero for all the steps.
Init: dp[0][i][j] = 1
Transition: dp[k][i][j] = sum(dp[k-1][i+dy][j+dx]) 8 ways of move from last step.
ans = sum(dp[k])
Time complexity: O(kmn) or O(k*12*8) = O(k)
Space complexity: O(kmn) -> O(12 * 8) = O(1)
Approach #2: DP. [C++]
class Solution {
public:
int knightDialer(int N) {
vector<vector<int>> dp(4, vector<int>(3, 1));
dp[3][0] = dp[3][2] = 0;
int mod = pow(10, 9) + 7;
vector<pair<int, int>> dirs = {{1, 2}, {1, -2}, {2, 1}, {2, -1},
{-1, 2}, {-1, -2}, {-2, 1}, {-2, -1}};
for (int k = 2; k <= N; ++k) {
vector<vector<int>> temp(4, vector<int>(3, 0));
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 3; ++j) {
if (i == 3 && j != 1) continue;
for (int k = 0; k < 8; ++k) {
int x_ = i + dirs[k].first;
int y_ = j + dirs[k].second;
if (x_ < 0 || y_ < 0 || x_ >= 4 || y_ >= 3) continue;
temp[i][j] = (temp[i][j] + dp[x_][y_]) % mod;
}
}
}
dp.swap(temp);
}
int ans = 0;
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 3; ++j) {
ans = (ans + dp[i][j]) % mod;
}
}
return ans;
}
};
define dp[k][i] as of ways to dial and the last key is i after k steps
init: dp[0][0:10] = 1
translation: dp[k][i] = sum(dp[k-1][j]) that j can move to i
ans: sum(dp[k])
Time complexity: O(k*10) = O(k)
Space complexity: O(k*10) -> O(10) = O(1).
Reference:
https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-935-knight-dialer/
935. Knight Dialer的更多相关文章
- [LeetCode] 935. Knight Dialer 骑士拨号器
A chess knight can move as indicated in the chess diagram below: . This time, we place o ...
- LeetCode 935. Knight Dialer
原题链接在这里:https://leetcode.com/problems/knight-dialer/ 题目: A chess knight can move as indicated in the ...
- 【leetcode】935. Knight Dialer
题目如下: A chess knight can move as indicated in the chess diagram below: . This time, we p ...
- 【LeetCode】935. Knight Dialer 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划TLE 空间换时间,利用对称性 优化空间复杂 ...
- [Swift]LeetCode935. 骑士拨号器 | Knight Dialer
A chess knight can move as indicated in the chess diagram below: . This time, we place o ...
- 109th LeetCode Weekly Contest Knight Dialer
A chess knight can move as indicated in the chess diagram below: . This time, we place o ...
- leetcode动态规划题目总结
Hello everyone, I am a Chinese noob programmer. I have practiced questions on leetcode.com for 2 yea ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- Android之Dialer之紧急号码
Android之Dialer之紧急号码 e over any other (e.g. supplementary service related) number analysis. a) 112 an ...
随机推荐
- jDeveloper运行慢
最近在使用 Jdeveloper 10.1.3.3 版本时发现速度奇慢无比,后经Google,发现如下解决方案:在 jdev.conf 文件的末尾加上如下两行,速度即可得到显著的提高. AddVMOp ...
- 使用第三方容器服务,自动化部署.Net Core
1.为什么用第三方,而不自建,有哪些第三方,最后实现的效果 a.尝试过自建,并成功了,但是很麻烦,要敲一堆命令,无法达到全自动化部署的要求. b.自建,就算用第三方的镜像包,感觉下载还是不快,不知道为 ...
- linux下memcache安装
安装配置 1. 安装libevent # tar zxf libevent-1.4.6-stable.tar.gz # cd libevent-1.4.6-stable # ./configure # ...
- Linux sar
一.简介 sar(System Activity Reporter系统活动情况报告)是目前 Linux 上最为全面的系统性能分析工具之一,可以从多方面对系统的活动进行报告,包括:文件的读写情况.系统调 ...
- Jmeter中控制某一段脚本失败后重复执行,并在每个HTTP Request名字中加上循环次数
================================================== 1.While Controller之前有一个BeanShell Sampler,用于Init N ...
- 第三次Scrum编码冲刺!!!
第三次冲刺 一.第三次冲刺任务 ! 在已有的基础上实现图书馆管理员对图书信息的查询以及对图书借阅情况的查询. 二.用户故事 本次的用户是图书馆的管理员 用户输入对应的管理员的账号和密码 用户选择图书 ...
- 乞丐版servlet容器第3篇
4 EventListener接口 让我们继续看SocketConnector中的acceptConnect方法: @Override protected void acceptConnect() t ...
- 2018.08.22 codves2370 小机房的树(lca+树上差分)
传送门 一道板子题. 直接树链剖分维护树上lca然后差分就行了. 代码: #include<bits/stdc++.h> #define N 50005 #define lc (p< ...
- try-catch+thows异常范围说明
方式一: CatalogPO deleteTarget = null; /** 查询是否存在 **/ deleteTarget = catalogMapper.findByCatalogId(cata ...
- BeanUtils.populate的方法的作用
BeanUtils位于org.apache.commons.beanutils.BeanUtils下面,其方法populate的作用解释如下: 完整方法: BeanUtils.populate( Ob ...
. 