Skip to content

Commit b30dc76

Browse files
authored
Create 2267.Check-if-There-Is-a-Valid-Parentheses-String-Path_v2.cpp
1 parent 728f006 commit b30dc76

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
bool dp[101][101][103];
3+
public:
4+
bool hasValidPath(vector<vector<char>>& grid)
5+
{
6+
int m = grid.size(), n = grid[0].size();
7+
8+
if (grid[0][0]=='(')
9+
dp[0][0][1] = true;
10+
11+
for (int i=0; i<m; i++)
12+
for (int j=0; j<n; j++)
13+
for (int k=0; k<=(m+n-1)/2; k++)
14+
{
15+
if (i==0 && j==0) continue;
16+
17+
if (k>0 && grid[i][j]=='(')
18+
dp[i][j][k] = (i>=1 && dp[i-1][j][k-1]) || (j>=1 && dp[i][j-1][k-1]);
19+
else if (grid[i][j]==')')
20+
dp[i][j][k] = (i>=1 && dp[i-1][j][k+1]) || (j>=1 && dp[i][j-1][k+1]);
21+
}
22+
23+
return dp[m-1][n-1][0];
24+
25+
}
26+
};

0 commit comments

Comments
 (0)