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 ...
随机推荐
- libevent源码分析一--io事件响应
这篇文章将分析libevent如何组织io事件,如何捕捉事件的发生并进行相应的响应.这里不会详细分析event与event_base的细节,仅描述io事件如何存储与如何响应. 1. select l ...
- UI单据按钮点击事件校验
一.按钮点击前事务处理<BeforeEventProcess> public override void BeforeEventProcess(IPart part, string eve ...
- python 2种创建多线程的方法
多个线程是可以操作同一个全局变量的,因此,可以通过这种方式来判断所有线程的执行进度 # 第一种方法:将要执行的方法作为参数传给Thread的构造方法 import threading import t ...
- Java知识回顾 (18)Java 8、9、11的新特性
Java 8 Java 8 (又称为 jdk 1.8) 是 Java 语言开发的一个主要版本. Oracle 公司于 2014 年 3 月 18 日发布 Java 8 ,它支持函数式编程,新的 Jav ...
- vue页面跳转拦截器
登录拦截逻辑 第一步:路由拦截 首先在定义路由的时候就需要多添加一个自定义字段requireAuth,用于判断该路由的访问是否需要登录.如果用户已经登录,则顺利进入路由, 否则就进入登录页面.在路由管 ...
- Mac版StarUML破解方法
StarUML是用nodejs写的.确切的说是用Electron前端框架写的.新版本中所有的starUML源代码是通过asar工具打包而成.确切的代码位置在“%LOCALAPPDATA%\Progra ...
- ugui用户定义操作按键
界面很简单,只创建了一Image,Image下边有一个Text.基本思路是点击Image,Text清空,进入修改状态,然后用户按下任意键,按下的任意键极为修改后的键 然后下面的脚本是挂在Image下面 ...
- python smtp登陆邮箱失败
>>> server.connect('smtp.163.com') (220, b'163.com Anti-spam GT for Coremail System (163com ...
- 搭建nginx做文件下载服务器
一.安装nginx yum install -y nginx 二.修改配置文件/etc/nginx/nginx.conf user nginx; worker_processes auto; erro ...
- nodejs 删除空文件
var fs = require("fs") var path = require("path") var listRealPath = path.resolv ...