原题链接在这里: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的更多相关文章

  1. [LeetCode] 935. Knight Dialer 骑士拨号器

    A chess knight can move as indicated in the chess diagram below:  .            This time, we place o ...

  2. 【LeetCode】935. Knight Dialer 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划TLE 空间换时间,利用对称性 优化空间复杂 ...

  3. 【leetcode】935. Knight Dialer

    题目如下: A chess knight can move as indicated in the chess diagram below:  .            This time, we p ...

  4. 935. Knight Dialer

    A chess knight can move as indicated in the chess diagram below:  .            This time, we place o ...

  5. 109th LeetCode Weekly Contest Knight Dialer

    A chess knight can move as indicated in the chess diagram below:  .            This time, we place o ...

  6. [Swift]LeetCode935. 骑士拨号器 | Knight Dialer

    A chess knight can move as indicated in the chess diagram below:  .            This time, we place o ...

  7. LeetCode 688. Knight Probability in Chessboard

    原题链接在这里:https://leetcode.com/problems/knight-probability-in-chessboard/description/ 题目: On an NxN ch ...

  8. LeetCode——688. Knight Probability in Chessboard

    一.题目链接:https://leetcode.com/problems/knight-probability-in-chessboard/ 二.题目大意: 给定一个N*N的棋盘和一个初始坐标值(r, ...

  9. leetcode动态规划题目总结

    Hello everyone, I am a Chinese noob programmer. I have practiced questions on leetcode.com for 2 yea ...

随机推荐

  1. yzoj 2372 小B的数字 题解

    题意 判断是否存在一个序列 $ b_i $ 使得 $ \prod_{i = 1}^{n} b_i  | b_i^{a_i}$ 恒成立,其中 $ b_i $ 中的每个数都是2的正整数次幂. 样例输入 3 ...

  2. composer安装FOSUserBundle内存溢出

    内存溢出异常: Fatal error: Allowed memory size of 2147483648 bytes exhausted (tried to allocate 4096 bytes ...

  3. Linux目录结构(目录结构详解是重点)

    1.Linux目录与Windows目录对比 1.Windows目录结构 2.Linux目录结构 深刻理解Linux 树状文件目录是非常重要的,只有记住他们,你才能在命令行中任意切换,想去哪里去哪里 2 ...

  4. C语言指针的一些用法

    指针是C语言的灵魂,精华之所在.指针强大而危险,用得好是一大利器,用得不好是一大潜在危害.正是指针具有强大而又危险的特性,加上指针比较难,很多人用的不好,所以越是封装程度高的语言,越是没有指针的&qu ...

  5. golang http及其他标准库

  6. 架构 MVC MVP MVVM 简介 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  7. sleep方法动态打印 C语言

    #include<Windows.h> #include<stdio.h> #include<stdlib.h> #include<string.h> ...

  8. Visual Studio 2019 安装

    目录 写在前面 官网下载 安装 等待安装 启动 写在前面 目前工作的开发环境还是旧版本的Visual Studio 2013版.个人感觉还是有点跟不上时代更新迭代的节奏了.毕竟,技术在进步.如果我们也 ...

  9. win10系统驱动备份及还原

    win10系统驱动备份及还原 方法如下: 1.右键单击开始按钮,选择“命令提示符(管理员)” 2.输入如下命令后按回车,等待备份完成: dism /online /export-driver /des ...

  10. visual studio 2015 开发时常见问题的解决方案

    1.visual studio 2015 用printf函数打印时来不及看结果窗口就关闭 方案一 在所写的代码后面,加上system("PAUSE"); 如下: