-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution935.cs
60 lines (52 loc) · 1.57 KB
/
Solution935.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System.Text;
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution935
{
/// <summary>
/// 935. Knight Dialer - Medium
/// <a href="https://leetcode.com/problems/knight-dialer">See the problem</a>
/// </summary>
public int KnightDialer(int n)
{
const int MOD = 1_000_000_007;
// Adjacency list for valid knight moves
int[][] moves = [
[4, 6], // From 0
[6, 8], // From 1
[7, 9], // From 2
[4, 8], // From 3
[0, 3, 9], // From 4
[], // From 5 (invalid)
[0, 1, 7], // From 6
[2, 6], // From 7
[1, 3], // From 8
[2, 4] // From 9
];
// DP array: dp[length][digit]
var dp = new int[n + 1, 10];
// Base case: at length 1, each digit can be used once
for (int digit = 0; digit <= 9; digit++)
{
dp[1, digit] = 1;
}
// Fill DP table for lengths 2 to n
for (int len = 2; len <= n; len++)
{
for (int digit = 0; digit <= 9; digit++)
{
foreach (int next in moves[digit])
{
dp[len, digit] = (dp[len, digit] + dp[len - 1, next]) % MOD;
}
}
}
// Sum up all numbers of length n
int result = 0;
for (int digit = 0; digit <= 9; digit++)
{
result = (result + dp[n, digit]) % MOD;
}
return result;
}
}