From 8ea905d1620c2b4d7e48b2f23b3d8beaa83638b3 Mon Sep 17 00:00:00 2001 From: Pritom Date: Wed, 24 Aug 2022 00:15:05 +0600 Subject: [PATCH] Adding 62-Unique-Paths.c --- c/62-Unique-Paths.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 c/62-Unique-Paths.c diff --git a/c/62-Unique-Paths.c b/c/62-Unique-Paths.c new file mode 100644 index 000000000..a72320b61 --- /dev/null +++ b/c/62-Unique-Paths.c @@ -0,0 +1,30 @@ +int dp[105][105] = {}; + +int ways(int posX, int posY, int m, int n) +{ + if(posX == m-1 && posY == n-1) return 1; + + if(posX >= m || posY >= n) return 0; + + if(dp[posX][posY] != -1) return dp[posX][posY]; + + int right = ways(posX + 1, posY, m, n); // moves to right corner. + + int bottom = ways(posX, posY + 1, m, n);// moves to bottom corner. + + dp[posX][posY] = right + bottom; + + return dp[posX][posY]; +} + +int uniquePaths(int m, int n) { + + // Initializing the dp array with -1 value. + for(int i = 0; i < 105; i++){ + for(int j = 0; j < 105; j++) dp[i][j] = -1; + } + + int ans = ways(0, 0, m, n); + + return ans; +} \ No newline at end of file