-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path178. MobileNumericKeypad.java
More file actions
30 lines (26 loc) · 1 KB
/
178. MobileNumericKeypad.java
File metadata and controls
30 lines (26 loc) · 1 KB
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
class Solution {
private int solve(int[][] keyPad, int i, int j, int n, int[][][] dp){
if(i < 0 || j < 0 || i == 4 || j == 3 || keyPad[i][j] == -1) return 0;
if(n == 1) return 1;
if(dp[i][j][n] != -1) return dp[i][j][n];
int self = solve(keyPad, i, j, n-1, dp);
int left = solve(keyPad, i, j-1, n-1, dp);
int right = solve(keyPad, i, j+1, n-1, dp);
int upper = solve(keyPad, i-1, j, n-1, dp);
int lower = solve(keyPad, i+1, j, n-1, dp);
return dp[i][j][n] = self + left + right + upper + lower;
}
public int getCount(int n) {
// code here
int[][] keyPad = {{1,2,3}, {4,5,6}, {7,8,9}, {-1,0,-1}};
int[][][] dp = new int[4][3][n+1];
for(int[][] row : dp) for(int[] row1 : row) Arrays.fill(row1, -1);
int ans = 0;
for(int i=0; i<4; i++){
for(int j=0; j<3; j++){
ans += solve(keyPad, i, j, n, dp);
}
}
return ans;
}
}