LeetCode 935. Knight Dialer
原题链接在这里:https://leetcode.com/problems/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
题解:
The question asks distinct numbers could dial.
It is actually the sum of ways jump ending at each cell.
Cell 1 could jump to cell 6 and 8. Thus accumlate the current ways count to next ways at 6 and 8.
Eventually, get all the sum.
Time Complexity: O(N).
Space: O(1).
AC Java:
class Solution {
public int knightDialer(int N) {
if(N == 0){
return 0;
} if(N == 1){
return 10;
} int M = 1000000007;
long [] cur = new long[10];
Arrays.fill(cur, 1);
for(int k = 2; k<=N; k++){
long [] next = new long[10]; next[1] = (cur[6]+cur[8])%M;
next[2] = (cur[7]+cur[9])%M;
next[3] = (cur[4]+cur[8])%M;
next[4] = (cur[3]+cur[9]+cur[0])%M;
next[5] = 0;
next[6] = (cur[1]+cur[7]+cur[0])%M;
next[7] = (cur[2]+cur[6])%M;
next[8] = (cur[1]+cur[3])%M;
next[9] = (cur[2]+cur[4])%M;
next[0] = (cur[4]+cur[6])%M; cur = next;
} long res = 0;
for(int i = 0; i<10; i++){
res = (res + cur[i]) % M;
}
return (int)res;
}
}
类似Number of Ways to Stay in the Same Place After Some Steps.
LeetCode 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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划TLE 空间换时间,利用对称性 优化空间复杂 ...
- 【leetcode】935. Knight Dialer
题目如下: A chess knight can move as indicated in the chess diagram below: . This time, we p ...
- 935. 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 ...
- [Swift]LeetCode935. 骑士拨号器 | Knight Dialer
A chess knight can move as indicated in the chess diagram below: . This time, we place o ...
- LeetCode 688. Knight Probability in Chessboard
原题链接在这里:https://leetcode.com/problems/knight-probability-in-chessboard/description/ 题目: On an NxN ch ...
- LeetCode——688. Knight Probability in Chessboard
一.题目链接:https://leetcode.com/problems/knight-probability-in-chessboard/ 二.题目大意: 给定一个N*N的棋盘和一个初始坐标值(r, ...
- leetcode动态规划题目总结
Hello everyone, I am a Chinese noob programmer. I have practiced questions on leetcode.com for 2 yea ...
随机推荐
- c++11多线程记录6:条件变量(condition variables)
https://www.youtube.com/watch?v=13dFggo4t_I视频地址 实例1 考虑这样一个场景:存在一个全局队列deque,线程A向deque中推入数据(写),线程B从deq ...
- SAS学习笔记1
数据采样 简单随机抽样,从sashelp数据集中air数据文件中选取30个数 数据探索 数字特征的探索:均值.频数.最大值.最小值.众数.中位数.方差.标准差 数字分布的探索:是否服从正态分布 连续型 ...
- 配置linux命令行界面的 文件显示颜色
在linux命令行界面下使用ls命令时,有时会看见显示的文件会有不同的颜色,因为linux的文件没有后缀名这个概念(Windows系统中的文件会有后缀名,从而可以将文件标识为不同类型),显示不同的颜色 ...
- windows 系统防火墙 添加端口号方法
目前在大部分公司内使用的台式机和部分服务器都采用了Windows操作系统,而我么都知道相当一部分病毒.恶意程序.黑客都是利用扫描端口号,利用开放的端口进行入侵,此时大型企业都会将服务器的系统防火墙打开 ...
- Sqlmap注入工具
Sqlmap注入工具 http://sqlmap.org/ Sqlmap是国外的一个免费的注入工具,基于python开发,支持现在几乎所有的数据库,支持get.post.cookie注入,可以添加co ...
- redux核心知识
Provider 作用:把父组件传递进来的store对象放入react 上下文中,这样connect组件就可以从上下文中获取到store对象 Connect 作用: 1.从react上下文中取出s ...
- 英语dyamaund钻石
dyamaund 英文词汇,中文翻译为金刚石的;镶钻;用钻石装饰 中文名:镶钻;钻石装饰 外文名:dyamaund 目录 释义 dyamaund 读音:[ˈdaɪəmənd, ˈdaɪmənd] ...
- ECharts大屏可视化【词云,堆积柱状图,折线图,南丁格尔玫瑰图】
一.简介 参考ECharts快速入门:https://www.cnblogs.com/yszd/p/11166048.html 二.代码实现 <!DOCTYPE html> <htm ...
- css浮动float详解
https://www.cnblogs.com/iyangyuan/archive/2013/03/27/2983813.html
- 缓存注解@Cacheable、@CacheEvict、@CachePut使用及注解失效时间
从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对事务管理的支持.Spring Cache是作用在方法上的,其核心思想是这样的:当我们在调用一个缓存方法时会把该 ...