返回介绍

solution / 0900-0999 / 0935.Knight Dialer / README_EN

发布于 2024-06-17 01:03:32 字数 6290 浏览 0 评论 0 收藏 0

935. Knight Dialer

中文文档

Description

The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagram:

A chess knight can move as indicated in the chess diagram below:

We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).

Given an integer n, return how many distinct phone numbers of length n we can dial.

You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.

As the answer may be very large, return the answer modulo 109 + 7.

 

Example 1:

Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.

Example 2:

Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]

Example 3:

Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.

 

Constraints:

  • 1 <= n <= 5000

Solutions

Solution 1

class Solution:
  def knightDialer(self, n: int) -> int:
    if n == 1:
      return 10
    f = [1] * 10
    for _ in range(n - 1):
      t = [0] * 10
      t[0] = f[4] + f[6]
      t[1] = f[6] + f[8]
      t[2] = f[7] + f[9]
      t[3] = f[4] + f[8]
      t[4] = f[0] + f[3] + f[9]
      t[6] = f[0] + f[1] + f[7]
      t[7] = f[2] + f[6]
      t[8] = f[1] + f[3]
      t[9] = f[2] + f[4]
      f = t
    return sum(t) % (10**9 + 7)
class Solution {
  private static final int MOD = (int) 1e9 + 7;

  public int knightDialer(int n) {
    if (n == 1) {
      return 10;
    }
    long[] f = new long[10];
    Arrays.fill(f, 1);
    while (--n > 0) {
      long[] t = new long[10];
      t[0] = f[4] + f[6];
      t[1] = f[6] + f[8];
      t[2] = f[7] + f[9];
      t[3] = f[4] + f[8];
      t[4] = f[0] + f[3] + f[9];
      t[6] = f[0] + f[1] + f[7];
      t[7] = f[2] + f[6];
      t[8] = f[1] + f[3];
      t[9] = f[2] + f[4];
      for (int i = 0; i < 10; ++i) {
        f[i] = t[i] % MOD;
      }
    }
    long ans = 0;
    for (long v : f) {
      ans = (ans + v) % MOD;
    }
    return (int) ans;
  }
}
using ll = long long;

class Solution {
public:
  int knightDialer(int n) {
    if (n == 1) return 10;
    int mod = 1e9 + 7;
    vector<ll> f(10, 1ll);
    while (--n) {
      vector<ll> t(10);
      t[0] = f[4] + f[6];
      t[1] = f[6] + f[8];
      t[2] = f[7] + f[9];
      t[3] = f[4] + f[8];
      t[4] = f[0] + f[3] + f[9];
      t[6] = f[0] + f[1] + f[7];
      t[7] = f[2] + f[6];
      t[8] = f[1] + f[3];
      t[9] = f[2] + f[4];
      for (int i = 0; i < 10; ++i) f[i] = t[i] % mod;
    }
    ll ans = accumulate(f.begin(), f.end(), 0ll);
    return (int) (ans % mod);
  }
};
func knightDialer(n int) int {
  if n == 1 {
    return 10
  }
  f := make([]int, 10)
  for i := range f {
    f[i] = 1
  }
  mod := int(1e9) + 7
  for i := 1; i < n; i++ {
    t := make([]int, 10)
    t[0] = f[4] + f[6]
    t[1] = f[6] + f[8]
    t[2] = f[7] + f[9]
    t[3] = f[4] + f[8]
    t[4] = f[0] + f[3] + f[9]
    t[6] = f[0] + f[1] + f[7]
    t[7] = f[2] + f[6]
    t[8] = f[1] + f[3]
    t[9] = f[2] + f[4]
    for j, v := range t {
      f[j] = v % mod
    }
  }
  ans := 0
  for _, v := range f {
    ans = (ans + v) % mod
  }
  return ans
}
function knightDialer(n: number): number {
  const MOD: number = 1e9 + 7;

  if (n === 1) {
    return 10;
  }

  const f: number[] = new Array(10).fill(1);

  while (--n > 0) {
    const t: number[] = new Array(10).fill(0);

    t[0] = f[4] + f[6];
    t[1] = f[6] + f[8];
    t[2] = f[7] + f[9];
    t[3] = f[4] + f[8];
    t[4] = f[0] + f[3] + f[9];
    t[6] = f[0] + f[1] + f[7];
    t[7] = f[2] + f[6];
    t[8] = f[1] + f[3];
    t[9] = f[2] + f[4];

    for (let i = 0; i < 10; ++i) {
      f[i] = t[i] % MOD;
    }
  }

  let ans: number = 0;
  for (const v of f) {
    ans = (ans + v) % MOD;
  }

  return ans;
}
public class Solution {
  public int KnightDialer(int n) {
    if (n == 1) return 10;
    int A = 4;
    int B = 2;
    int C = 2;
    int D = 1;
    int MOD = (int)1e9 + 7;
    for (int i = 0; i < n - 1; i++) {
      int tempA = A;
      int tempB = B;
      int tempC = C;
      int tempD = D;
      A = ((2 * tempB) % MOD + (2 * tempC) % MOD) % MOD;
      B = tempA;
      C = (tempA + (2 * tempD) % MOD) % MOD;
      D = tempC;
    }

    int ans = (A + B) % MOD;
    ans = (ans + C) % MOD;
    return (ans + D) % MOD;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文