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>
4
17
using namespace std ;
5
18
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
+ */
6
34
int solveMem (int i, int j, int m, int n, vector<vector<int >> &dp) {
7
35
if (i >= m || j >= n) return 0 ;
8
36
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) {
11
39
return dp[i][j];
12
40
}
13
41
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
+ */
14
48
int solveTab (int m, int n) {
15
49
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
18
52
for (int i = m - 2 ; i >= 0 ; i--) {
19
53
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
21
55
}
22
56
}
23
57
return dp[0 ][0 ];
24
58
}
25
59
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
+ */
26
66
int uniquePaths (int m, int n) {
27
67
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
31
72
73
+ /* *
74
+ * @brief Self-test implementations
75
+ */
32
76
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 );
37
81
cout << " All tests have successfully passed!\n " ;
38
82
}
39
83
84
+ /* *
85
+ * @brief Main function
86
+ * @returns 0 on successful execution
87
+ */
40
88
int main () {
41
89
test (); // run self-tests
42
90
return 0 ;
0 commit comments