Skip to content

Commit 5e5b2cd

Browse files
authored
Update unique_paths.cpp
1 parent eb39769 commit 5e5b2cd

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

dynamic_programming/unique_paths.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
* Approaches:
1111
* - **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
1313
* computation. Typically more intuitive and easy to write, but
1414
* relies on recursion and has associated call-stack overhead.
1515
*
@@ -37,14 +37,14 @@ namespace dynamic_programming {
3737
*/
3838
class UniquePathsSolver {
3939
private:
40-
std::vector<std::vector<int>> dp; ///< Memoization table
40+
std::vector<std::vector<int>> memoization_table; ///< Memoization table
4141
int m, n;
4242

4343
/**
4444
* @brief Bottom-up Tabulation solution.
4545
* @return int Number of unique paths from (0, 0) to (m-1, n-1)
4646
*/
47-
int solveTab() {
47+
int solveTabulation() {
4848
std::vector<std::vector<int>> table(m, std::vector<int>(n, 0));
4949

5050
for (int i = 0; i < m; i++) table[i][n - 1] = 1; ///< last column
@@ -60,10 +60,10 @@ class UniquePathsSolver {
6060

6161
public:
6262
/**
63-
* @brief Constructor initializes dimensions and memo table
63+
* @brief Constructor initializes dimensions and memoization table
6464
*/
6565
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));
6767
}
6868

6969
/**
@@ -72,16 +72,17 @@ class UniquePathsSolver {
7272
int uniquePathsMemo(int i = 0, int j = 0) {
7373
if (i >= m || j >= n) return 0;
7474
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);
7676

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);
7980
}
8081

8182
/**
8283
* @brief Get number of unique paths using Tabulation (Bottom-Up)
8384
*/
84-
int uniquePathsTab() { return solveTab(); }
85+
int uniquePathsTabulation() { return solveTabulation(); }
8586
};
8687

8788
} // namespace dynamic_programming
@@ -94,19 +95,19 @@ static void test() {
9495

9596
UniquePathsSolver solver1(3, 7);
9697
assert(solver1.uniquePathsMemo() == 28);
97-
assert(solver1.uniquePathsTab() == 28);
98+
assert(solver1.uniquePathsTabulation() == 28);
9899

99100
UniquePathsSolver solver2(3, 2);
100101
assert(solver2.uniquePathsMemo() == 3);
101-
assert(solver2.uniquePathsTab() == 3);
102+
assert(solver2.uniquePathsTabulation() == 3);
102103

103104
UniquePathsSolver solver3(1, 1);
104105
assert(solver3.uniquePathsMemo() == 1);
105-
assert(solver3.uniquePathsTab() == 1);
106+
assert(solver3.uniquePathsTabulation() == 1);
106107

107108
UniquePathsSolver solver4(2, 2);
108109
assert(solver4.uniquePathsMemo() == 2);
109-
assert(solver4.uniquePathsTab() == 2);
110+
assert(solver4.uniquePathsTabulation() == 2);
110111

111112
std::cout << "All tests have successfully passed!\n";
112113
}

0 commit comments

Comments
 (0)