Skip to content

Commit e27833b

Browse files
authored
Merge pull request neetcode-gh#904 from PritomKarmokar/c-solution-unique-path
Adding 62-Unique-Paths.c
2 parents 9ae9321 + 8ea905d commit e27833b

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

c/62-Unique-Paths.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
int dp[105][105] = {};
2+
3+
int ways(int posX, int posY, int m, int n)
4+
{
5+
if(posX == m-1 && posY == n-1) return 1;
6+
7+
if(posX >= m || posY >= n) return 0;
8+
9+
if(dp[posX][posY] != -1) return dp[posX][posY];
10+
11+
int right = ways(posX + 1, posY, m, n); // moves to right corner.
12+
13+
int bottom = ways(posX, posY + 1, m, n);// moves to bottom corner.
14+
15+
dp[posX][posY] = right + bottom;
16+
17+
return dp[posX][posY];
18+
}
19+
20+
int uniquePaths(int m, int n) {
21+
22+
// Initializing the dp array with -1 value.
23+
for(int i = 0; i < 105; i++){
24+
for(int j = 0; j < 105; j++) dp[i][j] = -1;
25+
}
26+
27+
int ans = ways(0, 0, m, n);
28+
29+
return ans;
30+
}

0 commit comments

Comments
 (0)