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 ...
随机推荐
- python+selenium实现自动化百度搜索关键词
通过python配合爬虫接口利用selenium实现自动化打开chrome浏览器,进行百度关键词搜索. 1.安装python3,访问官网选择对应的版本安装即可,最新版为3.7. 2.安装seleniu ...
- 类的练习——python编程从入门到实践
9-1 餐馆:创建一个名为Restaurant的类,其方法__init__()设置两个属性:restaurant_name和cuisine_type.创建一个名为describe_restaurant ...
- 【转】ZYNQ中三种实现GPIO的方式
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/husipeng86/article/det ...
- ubuntu 安装Jenkins
一.介绍 Jenkins是一款开源自动化服务器,旨在自动化连续集成和交付软件所涉及的重复技术任务. Jenkins是基于Java的,可以从Ubuntu软件包安装,也可以通过下载和运行其Web应用程序A ...
- CF933E A Preponderant Reunion DP
传送门 题解搬运工 设原问题为问题A.每一次减少\(\min\{p_i , p_{i+1}\}\)难以处理,我们考虑将限制变得宽松一些:每一次可以减少\([1,\min\{p_i , p_{i+1}\ ...
- php中的for循环和js中的for循环
php中的for循环 循环100个0 for ($i=0;$i<=100;$i++){ $pnums.='0'.","; } js中的for循环,循环31个相同的数.循环日期 ...
- 以yarn-client方式提交spark任务,任务一直卡在ACCEPTED状态
问题背景 spark是以客户端的方式安装的,并没有启动spark的mesos集群,这时候的spark就相当与hive客户端. 以local模型和yarn-cluster方式提交任务,都能正确额执行,但 ...
- vue项目中添加单元测试
从网上找了很多例子关于单元测试,都是如何新建项目的时候的添加单元测试,用vue-cli中怎么添加,但是我的项目已经生成了,不能再一次重新初始化,这时如何添加单元测试,这里面遇到了好多坑,写在这里记录一 ...
- [Props] vue组件间的传值及校验
基本用法 Prop的基本用法很简单,只需要在子组件的Vue实例中定义该属性并把值设为目标属性的数组即可 Vue.component('child', { ... // 接收message props: ...
- JavaScript之控制标签css
控制标签css标签.style.样式='样式具体的值'如果样式出现中横线,如border-radius,将中横线去掉,中横线后面的单词首字母大写,写成borderRadius如果原来就要该样式,表示修 ...