-
Notifications
You must be signed in to change notification settings - Fork 0
/
935.knight-dialer.cpp
39 lines (34 loc) · 985 Bytes
/
935.knight-dialer.cpp
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
/*
* @lc app=leetcode id=935 lang=cpp
*
* [935] Knight Dialer
*/
// @lc code=start
class Solution {
public:
static const int mod = 1e9 + 7;
vector<vector<int>> MOVES = {
{4, 6}, {8, 6}, {7, 9}, {4, 8}, {3, 9, 0},
{}, {0, 1, 7}, {2, 6}, {1, 3}, {2, 4},
};
int cache[5001][10];
int knightDialer(int n) {
vector<int> nextNumbers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
return knightDialer(n, nextNumbers);
}
int knightDialer(int remaining, vector<int>& nextNumbers) {
if (remaining == 1) return nextNumbers.size();
int count = 0;
for (int nextNumber : nextNumbers) {
int cur = cache[remaining][nextNumber];
if (cur == 0) {
cur = knightDialer(remaining - 1, MOVES[nextNumber]);
cache[remaining][nextNumber] = cur;
}
count += cur;
count %= mod;
}
return count;
}
};
// @lc code=end