1
1
/* *
2
2
* @file
3
- * @brief Implementation of Unique Paths problem using Dynamic Programming.
3
+ * @brief Implementation of Unique Paths (LeetCode 62) using Dynamic Programming.
4
4
* @details
5
5
* A robot is located at the top-left corner of an m × n grid.
6
6
* The robot can move either down or right at any point in time.
7
7
* This program computes the total number of unique paths to reach
8
8
* the bottom-right corner.
9
9
*
10
+ * Note: This is **Unique Paths I** (no obstacles). Obstacles are handled in
11
+ * the separate problem **Unique Paths II**.
12
+ *
10
13
* Approaches:
11
14
* - **Memoization (Top-Down)**: Recursively explores solutions while
12
15
* storing intermediate results in a cache (`memoization_table`) to avoid redundant
@@ -37,8 +40,9 @@ namespace dynamic_programming {
37
40
*/
38
41
class UniquePathsSolver {
39
42
private:
40
- std::vector<std::vector<int >> memoization_table; // /< Memoization table
41
- int m, n;
43
+ std::vector<std::vector<int >> memoization_table; // /< Memoization table to cache intermediate results
44
+ std::size_t m; // /< Number of rows in the grid
45
+ std::size_t n; // /< Number of columns in the grid
42
46
43
47
/* *
44
48
* @brief Bottom-up Tabulation solution.
@@ -47,11 +51,11 @@ class UniquePathsSolver {
47
51
int solveTabulation () {
48
52
std::vector<std::vector<int >> table (m, std::vector<int >(n, 0 ));
49
53
50
- for (int i = 0 ; i < m; i++) table[i][n - 1 ] = 1 ; // /< last column
51
- for (int j = 0 ; j < n; j++) table[m - 1 ][j] = 1 ; // /< last row
54
+ for (std:: size_t i = 0 ; i < m; i++) table[i][n - 1 ] = 1 ; // /< last column
55
+ for (std:: size_t j = 0 ; j < n; j++) table[m - 1 ][j] = 1 ; // /< last row
52
56
53
- for (int i = m - 2 ; i >= 0 ; i--) {
54
- for (int j = n - 2 ; j >= 0 ; j--) {
57
+ for (int i = static_cast < int >(m) - 2 ; i >= 0 ; i--) {
58
+ for (int j = static_cast < int >(n) - 2 ; j >= 0 ; j--) {
55
59
table[i][j] = table[i + 1 ][j] + table[i][j + 1 ];
56
60
}
57
61
}
@@ -61,15 +65,20 @@ class UniquePathsSolver {
61
65
public:
62
66
/* *
63
67
* @brief Constructor initializes dimensions and memoization table
68
+ * @param rows number of rows in the grid (must be > 0)
69
+ * @param cols number of columns in the grid (must be > 0)
64
70
*/
65
- UniquePathsSolver (int rows, int cols) : m(rows), n(cols) {
71
+ UniquePathsSolver (std:: size_t rows, std:: size_t cols) : m(rows), n(cols) {
66
72
memoization_table.assign (m, std::vector<int >(n, -1 ));
67
73
}
68
74
69
75
/* *
70
76
* @brief Get number of unique paths using Memoization (Top-Down)
77
+ * @param i current row index (default = 0)
78
+ * @param j current column index (default = 0)
79
+ * @return int Number of unique paths from (i, j) to (m-1, n-1)
71
80
*/
72
- int uniquePathsMemo (int i = 0 , int j = 0 ) {
81
+ int uniquePathsMemo (std:: size_t i = 0 , std:: size_t j = 0 ) {
73
82
if (i >= m || j >= n) return 0 ;
74
83
if (i == m - 1 && j == n - 1 ) return 1 ;
75
84
if (memoization_table.at (i).at (j) != -1 ) return memoization_table.at (i).at (j);
@@ -81,6 +90,7 @@ class UniquePathsSolver {
81
90
82
91
/* *
83
92
* @brief Get number of unique paths using Tabulation (Bottom-Up)
93
+ * @return int Number of unique paths from (0, 0) to (m-1, n-1)
84
94
*/
85
95
int uniquePathsTabulation () { return solveTabulation (); }
86
96
};
0 commit comments