-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCountOfPathsInGrid.java
More file actions
72 lines (49 loc) · 1.34 KB
/
CountOfPathsInGrid.java
File metadata and controls
72 lines (49 loc) · 1.34 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
Count of paths in a grid
Given an integer A, find and return the number of paths in a grid of size (A x A) that starts from (1, 1) and reaches (A, A) without crossing the major diagonal.
Since the result can be large, return the result modulo (10^9 + 7).
NOTE
The major diagonal of a matrix A is the collection of entries A[i][j] where i == j
Input Format
The only argument given is integer A.
Output Format
Return the number of paths modulo (10^9 + 7).
Constraints
1 <= A <= 10^6
For Example
Input 1:
A = 2
Output 1:
1
Input 2:
A = 5
Output 2:
14
*/
public class Solution {
public int solve(int A) {
int mod = 1000000007;
long totalPath = ncr(2*A-2, A-1, mod);
long faultPath = ncr(2*A-2, A-2, mod);
return (int)((totalPath-faultPath+mod)%mod);
}
public long ncr(int n, int r, int p){
long[] fact = new long[n+1];
fact[0] = 1;
for(int i=1;i<=n;i++){
fact[i] = (i*fact[i-1])%p;
}
return (fact[n]*power(fact[n-r], p-2, p)%p)*power(fact[r], p-2, p)%p;
}
public long power(long a, long b, long p){
if(b==1){
return a;
}
long temp = power(a, b/2, p);
temp = (temp*temp)%p;
if(b%2!=0){
temp = (temp*a)%p;
}
return temp;
}
}