-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
52 lines (46 loc) · 1.32 KB
/
Copy pathSolution.java
File metadata and controls
52 lines (46 loc) · 1.32 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.util.HashMap;
import java.util.Map;
class Solution
{
int MOD = 1000000007;
public static void main(String args[])
{
Solution sol = new Solution();
System.out.println(sol.numWays(27, 7));
}
public int numWays(int steps, int arrLen)
{
Map<Integer, Map<Integer, Long>> cache = new HashMap<>();
return numWays(0, steps, arrLen, cache);
}
private int numWays(final int currPos, final int steps, final int arrLen, Map<Integer, Map<Integer, Long>> cache)
{
if (steps == 0)
{
return currPos == 0 ? 1 : 0;
}
if (currPos > arrLen - 1 || currPos < 0)
{
return 0;
}
if (cache.containsKey(currPos) && cache.get(currPos).containsKey(steps))
{
return (int) (cache.get(currPos).get(steps) % MOD);
}
long count = 0;
for (int j : new int[]{1, 0, -1})
{
if (j == 1 && (currPos + 1 > arrLen - 1))
{
continue;
}
if (j == -1 && (currPos - 1 < 0))
{
continue;
}
count += numWays(currPos + j, steps - 1, arrLen, cache);
}
cache.computeIfAbsent(currPos, HashMap::new).put(steps, count);
return (int) (count % MOD);
}
}