1
+ // Runtime: 111 ms (Top 23.90%) | Memory: 31.1 MB (Top 16.67%)
1
2
class Solution {
2
3
public:
3
4
int mod=1e9 +7 ;
4
5
map<pair<int ,int >,pair<int ,int >>h;
5
6
pair<int ,int >solve (vector<string>&board,int i,int j,int n,int m)
6
7
{
7
- // base case if you reach top left then 1 path hence return 1
8
+ // base case if you reach top left then 1 path hence return 1
8
9
if (i==0 && j==0 )return {0 ,1 };
9
- // return 0 as no path is detected
10
+ // return 0 as no path is detected
10
11
if (i<0 || j<0 || i>=n || j>=m || board[i][j]==' X' )return {INT_MIN,0 };
11
- // check if it is stored or not
12
+ // check if it is stored or not
12
13
if (h.find ({i,j})!=h.end ())return h[{i,j}];
13
14
int no=0 ,cnt=0 ;
14
15
if (board[i][j]!=' S' )no=board[i][j]-' 0' ;
15
- // top ,left ,top left
16
+ // top ,left ,top left
16
17
auto a=solve (board,i-1 ,j,n,m);
17
18
auto b=solve (board,i,j-1 ,n,m);
18
19
auto c=solve (board,i-1 ,j-1 ,n,m);
19
- // maxi ans
20
+ // maxi ans
20
21
int curr=(max (a.first ,max (b.first ,c.first )))%mod;
21
- // if maxi ans == a , b , c then increament count of a ,b,c
22
+ // if maxi ans == a , b , c then increament count of a ,b,c
22
23
if (curr==a.first )cnt+=a.second ;
23
24
if (curr==b.first )cnt+=b.second ;
24
25
if (curr==c.first )cnt+=c.second ;
@@ -29,4 +30,4 @@ class Solution {
29
30
if (ans.first <0 )return {0 ,0 };
30
31
return {ans.first %mod,ans.second %mod};
31
32
}
32
- };
33
+ };
0 commit comments