Skip to content

Commit d7f9c60

Browse files
authored
Update unique_paths.cpp
1 parent 35fe129 commit d7f9c60

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

dynamic_programming/unique_paths.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
/**
22
* @file
3-
* @brief Implementation of Unique Paths problem using Dynamic Programming.
3+
* @brief Implementation of Unique Paths (LeetCode 62) using Dynamic Programming.
44
* @details
55
* A robot is located at the top-left corner of an m × n grid.
66
* The robot can move either down or right at any point in time.
77
* This program computes the total number of unique paths to reach
88
* the bottom-right corner.
99
*
10+
* Note: This is **Unique Paths I** (no obstacles). Obstacles are handled in
11+
* the separate problem **Unique Paths II**.
12+
*
1013
* Approaches:
1114
* - **Memoization (Top-Down)**: Recursively explores solutions while
1215
* storing intermediate results in a cache (`memoization_table`) to avoid redundant
@@ -37,8 +40,9 @@ namespace dynamic_programming {
3740
*/
3841
class UniquePathsSolver {
3942
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
4246

4347
/**
4448
* @brief Bottom-up Tabulation solution.
@@ -47,11 +51,11 @@ class UniquePathsSolver {
4751
int solveTabulation() {
4852
std::vector<std::vector<int>> table(m, std::vector<int>(n, 0));
4953

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
5256

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--) {
5559
table[i][j] = table[i + 1][j] + table[i][j + 1];
5660
}
5761
}
@@ -61,15 +65,20 @@ class UniquePathsSolver {
6165
public:
6266
/**
6367
* @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)
6470
*/
65-
UniquePathsSolver(int rows, int cols) : m(rows), n(cols) {
71+
UniquePathsSolver(std::size_t rows, std::size_t cols) : m(rows), n(cols) {
6672
memoization_table.assign(m, std::vector<int>(n, -1));
6773
}
6874

6975
/**
7076
* @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)
7180
*/
72-
int uniquePathsMemo(int i = 0, int j = 0) {
81+
int uniquePathsMemo(std::size_t i = 0, std::size_t j = 0) {
7382
if (i >= m || j >= n) return 0;
7483
if (i == m - 1 && j == n - 1) return 1;
7584
if (memoization_table.at(i).at(j) != -1) return memoization_table.at(i).at(j);
@@ -81,6 +90,7 @@ class UniquePathsSolver {
8190

8291
/**
8392
* @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)
8494
*/
8595
int uniquePathsTabulation() { return solveTabulation(); }
8696
};

0 commit comments

Comments
 (0)