Skip to content

Commit ba22f9f

Browse files
authored
Update unique_paths.cpp
1 parent a73323e commit ba22f9f

File tree

1 file changed

+61
-13
lines changed

1 file changed

+61
-13
lines changed

dynamic_programming/unique_paths.cpp

Lines changed: 61 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,36 @@
1-
#include <iostream>
2-
#include <vector>
3-
#include <cassert>
1+
/**
2+
* @file
3+
* @brief Implementation of Unique Paths problem using
4+
* Dynamic Programming (Memoization + Tabulation).
5+
* @details
6+
* A robot is located at the top-left corner of an m x n grid.
7+
* The robot can move either down or right at any point in time.
8+
* The robot is trying to reach the bottom-right corner.
9+
* This program computes the total number of unique paths.
10+
*
11+
* @see https://leetcode.com/problems/unique-paths/
12+
*/
13+
14+
#include <iostream>
15+
#include <vector>
16+
#include <cassert>
417
using namespace std;
518

19+
/**
20+
* @namespace dp
21+
* @brief Dynamic Programming algorithms
22+
*/
23+
namespace dp {
24+
25+
/**
26+
* @brief Recursive + Memoization solution.
27+
* @param i Current row index
28+
* @param j Current column index
29+
* @param m Number of rows
30+
* @param n Number of columns
31+
* @param dp Memoization table
32+
* @return int Number of unique paths from (i, j) to (m-1, n-1)
33+
*/
634
int solveMem(int i, int j, int m, int n, vector<vector<int>> &dp) {
735
if (i >= m || j >= n) return 0;
836
if (i == m - 1 && j == n - 1) return 1;
@@ -11,32 +39,52 @@ int solveMem(int i, int j, int m, int n, vector<vector<int>> &dp) {
1139
return dp[i][j];
1240
}
1341

42+
/**
43+
* @brief Bottom-up Tabulation solution.
44+
* @param m Number of rows
45+
* @param n Number of columns
46+
* @return int Number of unique paths from (0, 0) to (m-1, n-1)
47+
*/
1448
int solveTab(int m, int n) {
1549
vector<vector<int>> dp(m, vector<int>(n, 0));
16-
for (int i = 0; i < m; i++) dp[i][n - 1] = 1; // last column paths = 1
17-
for (int j = 0; j < n; j++) dp[m - 1][j] = 1; // last row paths = 1
50+
for (int i = 0; i < m; i++) dp[i][n - 1] = 1; ///< last column paths = 1
51+
for (int j = 0; j < n; j++) dp[m - 1][j] = 1; ///< last row paths = 1
1852
for (int i = m - 2; i >= 0; i--) {
1953
for (int j = n - 2; j >= 0; j--) {
20-
dp[i][j] = dp[i + 1][j] + dp[i][j + 1]; // paths from right + down
54+
dp[i][j] = dp[i + 1][j] + dp[i][j + 1]; ///< from down + right
2155
}
2256
}
2357
return dp[0][0];
2458
}
2559

60+
/**
61+
* @brief Returns number of unique paths in an m x n grid.
62+
* @param m Number of rows
63+
* @param n Number of columns
64+
* @return int Total number of unique paths
65+
*/
2666
int uniquePaths(int m, int n) {
2767
vector<vector<int>> dp(m + 1, vector<int>(n + 1, -1));
28-
// return solveMem(0, 0, m, n, dp);
29-
return solveTab(m, n);
30-
}
68+
// return solveMem(0, 0, m, n, dp);
69+
return solveTab(m, n);
70+
71+
} // namespace dp
3172

73+
/**
74+
* @brief Self-test implementations
75+
*/
3276
static void test() {
33-
assert(uniquePaths(3, 7) == 28);
34-
assert(uniquePaths(3, 2) == 3);
35-
assert(uniquePaths(1, 1) == 1);
36-
assert(uniquePaths(2, 2) == 2);
77+
assert(dp::uniquePaths(3, 7) == 28);
78+
assert(dp::uniquePaths(3, 2) == 3);
79+
assert(dp::uniquePaths(1, 1) == 1);
80+
assert(dp::uniquePaths(2, 2) == 2);
3781
cout << "All tests have successfully passed!\n";
3882
}
3983

84+
/**
85+
* @brief Main function
86+
* @returns 0 on successful execution
87+
*/
4088
int main() {
4189
test(); // run self-tests
4290
return 0;

0 commit comments

Comments
 (0)