9
9
*
10
10
* Approaches:
11
11
* - **Memoization (Top-Down)**: Recursively explores solutions while
12
- * storing intermediate results in a cache (`dp `) to avoid redundant
12
+ * storing intermediate results in a cache (`memoization_table `) to avoid redundant
13
13
* computation. Typically more intuitive and easy to write, but
14
14
* relies on recursion and has associated call-stack overhead.
15
15
*
@@ -37,14 +37,14 @@ namespace dynamic_programming {
37
37
*/
38
38
class UniquePathsSolver {
39
39
private:
40
- std::vector<std::vector<int >> dp ; // /< Memoization table
40
+ std::vector<std::vector<int >> memoization_table ; // /< Memoization table
41
41
int m, n;
42
42
43
43
/* *
44
44
* @brief Bottom-up Tabulation solution.
45
45
* @return int Number of unique paths from (0, 0) to (m-1, n-1)
46
46
*/
47
- int solveTab () {
47
+ int solveTabulation () {
48
48
std::vector<std::vector<int >> table (m, std::vector<int >(n, 0 ));
49
49
50
50
for (int i = 0 ; i < m; i++) table[i][n - 1 ] = 1 ; // /< last column
@@ -60,10 +60,10 @@ class UniquePathsSolver {
60
60
61
61
public:
62
62
/* *
63
- * @brief Constructor initializes dimensions and memo table
63
+ * @brief Constructor initializes dimensions and memoization table
64
64
*/
65
65
UniquePathsSolver (int rows, int cols) : m(rows), n(cols) {
66
- dp .assign (m, std::vector<int >(n, -1 ));
66
+ memoization_table .assign (m, std::vector<int >(n, -1 ));
67
67
}
68
68
69
69
/* *
@@ -72,16 +72,17 @@ class UniquePathsSolver {
72
72
int uniquePathsMemo (int i = 0 , int j = 0 ) {
73
73
if (i >= m || j >= n) return 0 ;
74
74
if (i == m - 1 && j == n - 1 ) return 1 ;
75
- if (dp .at (i).at (j) != -1 ) return dp .at (i).at (j);
75
+ if (memoization_table .at (i).at (j) != -1 ) return memoization_table .at (i).at (j);
76
76
77
- dp.at (i).at (j) = uniquePathsMemo (i + 1 , j) + uniquePathsMemo (i, j + 1 );
78
- return dp.at (i).at (j);
77
+ memoization_table.at (i).at (j) =
78
+ uniquePathsMemo (i + 1 , j) + uniquePathsMemo (i, j + 1 );
79
+ return memoization_table.at (i).at (j);
79
80
}
80
81
81
82
/* *
82
83
* @brief Get number of unique paths using Tabulation (Bottom-Up)
83
84
*/
84
- int uniquePathsTab () { return solveTab (); }
85
+ int uniquePathsTabulation () { return solveTabulation (); }
85
86
};
86
87
87
88
} // namespace dynamic_programming
@@ -94,19 +95,19 @@ static void test() {
94
95
95
96
UniquePathsSolver solver1 (3 , 7 );
96
97
assert (solver1.uniquePathsMemo () == 28 );
97
- assert (solver1.uniquePathsTab () == 28 );
98
+ assert (solver1.uniquePathsTabulation () == 28 );
98
99
99
100
UniquePathsSolver solver2 (3 , 2 );
100
101
assert (solver2.uniquePathsMemo () == 3 );
101
- assert (solver2.uniquePathsTab () == 3 );
102
+ assert (solver2.uniquePathsTabulation () == 3 );
102
103
103
104
UniquePathsSolver solver3 (1 , 1 );
104
105
assert (solver3.uniquePathsMemo () == 1 );
105
- assert (solver3.uniquePathsTab () == 1 );
106
+ assert (solver3.uniquePathsTabulation () == 1 );
106
107
107
108
UniquePathsSolver solver4 (2 , 2 );
108
109
assert (solver4.uniquePathsMemo () == 2 );
109
- assert (solver4.uniquePathsTab () == 2 );
110
+ assert (solver4.uniquePathsTabulation () == 2 );
110
111
111
112
std::cout << " All tests have successfully passed!\n " ;
112
113
}
0 commit comments