From a73323e355c6a3ffa9a465473f0a592d570535c0 Mon Sep 17 00:00:00 2001 From: sayan Date: Tue, 2 Sep 2025 10:06:41 +0530 Subject: [PATCH 1/9] Adding dynamic programming solution for unique paths problem --- dynamic_programming/unique_paths.cpp | 43 ++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 dynamic_programming/unique_paths.cpp diff --git a/dynamic_programming/unique_paths.cpp b/dynamic_programming/unique_paths.cpp new file mode 100644 index 00000000000..9b59ee3ee2b --- /dev/null +++ b/dynamic_programming/unique_paths.cpp @@ -0,0 +1,43 @@ +#include +#include +#include +using namespace std; + +int solveMem(int i, int j, int m, int n, vector> &dp) { + if (i>= m || j>= n) return 0; + if (i == m - 1 && j == n - 1) return 1; + if (dp[i][j] != -1) return dp[i][j]; + dp[i][j] = solveMem(i + 1, j, m, n, dp) + solveMem(i, j + 1, m, n, dp); + return dp[i][j]; +} + +int solveTab(int m, int n) { + vector> dp(m, vector(n, 0)); + for (int i = 0; i < m; i++) dp[i][n - 1] = 1; // last column paths = 1 + for (int j = 0; j < n; j++) dp[m - 1][j] = 1; // last row paths = 1 + for (int i = m - 2; i>= 0; i--) { + for (int j = n - 2; j>= 0; j--) { + dp[i][j] = dp[i + 1][j] + dp[i][j + 1]; // paths from right + down + } + } + return dp[0][0]; +} + +int uniquePaths(int m, int n) { + vector> dp(m + 1, vector(n + 1, -1)); + // return solveMem(0, 0, m, n, dp); + return solveTab(m, n); +} + +static void test() { + assert(uniquePaths(3, 7) == 28); + assert(uniquePaths(3, 2) == 3); + assert(uniquePaths(1, 1) == 1); + assert(uniquePaths(2, 2) == 2); + cout << "All tests have successfully passed!\n"; +} + +int main() { + test(); // run self-tests + return 0; +} From ba22f9f74e6f9c9f3d74717719e9a338b0e819cb Mon Sep 17 00:00:00 2001 From: Sayan Singh <157497677+singhsayan@users.noreply.github.com> Date: Tue, 2 Sep 2025 11:06:17 +0530 Subject: [PATCH 2/9] Update unique_paths.cpp --- dynamic_programming/unique_paths.cpp | 74 +++++++++++++++++++++++----- 1 file changed, 61 insertions(+), 13 deletions(-) diff --git a/dynamic_programming/unique_paths.cpp b/dynamic_programming/unique_paths.cpp index 9b59ee3ee2b..3eac81cb07a 100644 --- a/dynamic_programming/unique_paths.cpp +++ b/dynamic_programming/unique_paths.cpp @@ -1,8 +1,36 @@ -#include -#include -#include +/** + * @file + * @brief Implementation of Unique Paths problem using + * Dynamic Programming (Memoization + Tabulation). + * @details + * A robot is located at the top-left corner of an m x n grid. + * The robot can move either down or right at any point in time. + * The robot is trying to reach the bottom-right corner. + * This program computes the total number of unique paths. + * + * @see https://leetcode.com/problems/unique-paths/ + */ + +#include +#include +#include using namespace std; +/** + * @namespace dp + * @brief Dynamic Programming algorithms + */ +namespace dp { + +/** + * @brief Recursive + Memoization solution. + * @param i Current row index + * @param j Current column index + * @param m Number of rows + * @param n Number of columns + * @param dp Memoization table + * @return int Number of unique paths from (i, j) to (m-1, n-1) + */ int solveMem(int i, int j, int m, int n, vector> &dp) { if (i>= m || j>= n) return 0; if (i == m - 1 && j == n - 1) return 1; @@ -11,32 +39,52 @@ int solveMem(int i, int j, int m, int n, vector> &dp) { return dp[i][j]; } +/** + * @brief Bottom-up Tabulation solution. + * @param m Number of rows + * @param n Number of columns + * @return int Number of unique paths from (0, 0) to (m-1, n-1) + */ int solveTab(int m, int n) { vector> dp(m, vector(n, 0)); - for (int i = 0; i < m; i++) dp[i][n - 1] = 1; // last column paths = 1 - for (int j = 0; j < n; j++) dp[m - 1][j] = 1; // last row paths = 1 + for (int i = 0; i < m; i++) dp[i][n - 1] = 1; ///< last column paths = 1 + for (int j = 0; j < n; j++) dp[m - 1][j] = 1; ///< last row paths = 1 for (int i = m - 2; i>= 0; i--) { for (int j = n - 2; j>= 0; j--) { - dp[i][j] = dp[i + 1][j] + dp[i][j + 1]; // paths from right + down + dp[i][j] = dp[i + 1][j] + dp[i][j + 1]; ///< from down + right } } return dp[0][0]; } +/** + * @brief Returns number of unique paths in an m x n grid. + * @param m Number of rows + * @param n Number of columns + * @return int Total number of unique paths + */ int uniquePaths(int m, int n) { vector> dp(m + 1, vector(n + 1, -1)); - // return solveMem(0, 0, m, n, dp); - return solveTab(m, n); -} + // return solveMem(0, 0, m, n, dp); + return solveTab(m, n); + +} // namespace dp +/** + * @brief Self-test implementations + */ static void test() { - assert(uniquePaths(3, 7) == 28); - assert(uniquePaths(3, 2) == 3); - assert(uniquePaths(1, 1) == 1); - assert(uniquePaths(2, 2) == 2); + assert(dp::uniquePaths(3, 7) == 28); + assert(dp::uniquePaths(3, 2) == 3); + assert(dp::uniquePaths(1, 1) == 1); + assert(dp::uniquePaths(2, 2) == 2); cout << "All tests have successfully passed!\n"; } +/** + * @brief Main function + * @returns 0 on successful execution + */ int main() { test(); // run self-tests return 0; From d87ca7c978d121c38de4f9c7615f6d51d0ca0583 Mon Sep 17 00:00:00 2001 From: Sayan Singh <157497677+singhsayan@users.noreply.github.com> Date: Thu, 4 Sep 2025 12:19:24 +0530 Subject: [PATCH 3/9] Refactor Unique Paths as requested in code review --- dynamic_programming/unique_paths.cpp | 130 ++++++++++++++++----------- 1 file changed, 79 insertions(+), 51 deletions(-) diff --git a/dynamic_programming/unique_paths.cpp b/dynamic_programming/unique_paths.cpp index 3eac81cb07a..6a6d10665ae 100644 --- a/dynamic_programming/unique_paths.cpp +++ b/dynamic_programming/unique_paths.cpp @@ -11,74 +11,102 @@ * @see https://leetcode.com/problems/unique-paths/ */ -#include -#include -#include -using namespace std; +#include +#include +#include /** - * @namespace dp + * @namespace dynamic_programming * @brief Dynamic Programming algorithms */ -namespace dp { +namespace dynamic_programming { /** - * @brief Recursive + Memoization solution. - * @param i Current row index - * @param j Current column index - * @param m Number of rows - * @param n Number of columns - * @param dp Memoization table - * @return int Number of unique paths from (i, j) to (m-1, n-1) + * @class UniquePathsSolver + * @brief Solves the Unique Paths problem using both memoization and tabulation. */ -int solveMem(int i, int j, int m, int n, vector> &dp) { - if (i>= m || j>= n) return 0; - if (i == m - 1 && j == n - 1) return 1; - if (dp[i][j] != -1) return dp[i][j]; - dp[i][j] = solveMem(i + 1, j, m, n, dp) + solveMem(i, j + 1, m, n, dp); - return dp[i][j]; -} +class UniquePathsSolver { + private: + std::vector> dp; ///< Memoization table + int m, n; -/** - * @brief Bottom-up Tabulation solution. - * @param m Number of rows - * @param n Number of columns - * @return int Number of unique paths from (0, 0) to (m-1, n-1) - */ -int solveTab(int m, int n) { - vector> dp(m, vector(n, 0)); - for (int i = 0; i < m; i++) dp[i][n - 1] = 1; ///< last column paths = 1 - for (int j = 0; j < n; j++) dp[m - 1][j] = 1; ///< last row paths = 1 - for (int i = m - 2; i>= 0; i--) { - for (int j = n - 2; j>= 0; j--) { - dp[i][j] = dp[i + 1][j] + dp[i][j + 1]; ///< from down + right + /** + * @brief Recursive + Memoization solution. + * @param i Current row index + * @param j Current column index + * @return int Number of unique paths from (i, j) to (m-1, n-1) + */ + int solveMem(int i, int j) { + if (i>= m || j>= n) return 0; + if (i == m - 1 && j == n - 1) return 1; + if (dp.at(i).at(j) != -1) return dp.at(i).at(j); + + dp.at(i).at(j) = solveMem(i + 1, j) + solveMem(i, j + 1); + return dp.at(i).at(j); + } + + /** + * @brief Bottom-up Tabulation solution. + * @return int Number of unique paths from (0, 0) to (m-1, n-1) + */ + int solveTab() { + std::vector> table(m, std::vector(n, 0)); + + for (int i = 0; i < m; i++) table[i][n - 1] = 1; ///< last column + for (int j = 0; j < n; j++) table[m - 1][j] = 1; ///< last row + + for (int i = m - 2; i>= 0; i--) { + for (int j = n - 2; j>= 0; j--) { + table[i][j] = table[i + 1][j] + table[i][j + 1]; + } } + return table[0][0]; } - return dp[0][0]; -} -/** - * @brief Returns number of unique paths in an m x n grid. - * @param m Number of rows - * @param n Number of columns - * @return int Total number of unique paths - */ -int uniquePaths(int m, int n) { - vector> dp(m + 1, vector(n + 1, -1)); - // return solveMem(0, 0, m, n, dp); - return solveTab(m, n); + public: + /** + * @brief Constructor initializes dimensions and memo table + */ + UniquePathsSolver(int rows, int cols) : m(rows), n(cols) { + dp.assign(m, std::vector(n, -1)); + } + + /** + * @brief Get number of unique paths using Memoization + */ + int uniquePathsMemo() { return solveMem(0, 0); } -} // namespace dp + /** + * @brief Get number of unique paths using Tabulation + */ + int uniquePathsTab() { return solveTab(); } +}; + +} // namespace dynamic_programming /** * @brief Self-test implementations */ static void test() { - assert(dp::uniquePaths(3, 7) == 28); - assert(dp::uniquePaths(3, 2) == 3); - assert(dp::uniquePaths(1, 1) == 1); - assert(dp::uniquePaths(2, 2) == 2); - cout << "All tests have successfully passed!\n"; + using namespace dynamic_programming; + + UniquePathsSolver solver1(3, 7); + assert(solver1.uniquePathsMemo() == 28); + assert(solver1.uniquePathsTab() == 28); + + UniquePathsSolver solver2(3, 2); + assert(solver2.uniquePathsMemo() == 3); + assert(solver2.uniquePathsTab() == 3); + + UniquePathsSolver solver3(1, 1); + assert(solver3.uniquePathsMemo() == 1); + assert(solver3.uniquePathsTab() == 1); + + UniquePathsSolver solver4(2, 2); + assert(solver4.uniquePathsMemo() == 2); + assert(solver4.uniquePathsTab() == 2); + + std::cout << "All tests have successfully passed!\n"; } /** From 8dce8c631938befa4b8b07110b607a0c7d412378 Mon Sep 17 00:00:00 2001 From: Sayan Singh <157497677+singhsayan@users.noreply.github.com> Date: Thu, 4 Sep 2025 08:54:24 +0200 Subject: [PATCH 4/9] Update unique_paths.cpp --- dynamic_programming/unique_paths.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/unique_paths.cpp b/dynamic_programming/unique_paths.cpp index 6a6d10665ae..35e9c34a21f 100644 --- a/dynamic_programming/unique_paths.cpp +++ b/dynamic_programming/unique_paths.cpp @@ -88,7 +88,7 @@ class UniquePathsSolver { * @brief Self-test implementations */ static void test() { - using namespace dynamic_programming; + using namespace dp::dynamic_programming; UniquePathsSolver solver1(3, 7); assert(solver1.uniquePathsMemo() == 28); From 548c500455fcd80031db57b49a52ea1fbca7cb81 Mon Sep 17 00:00:00 2001 From: Sayan Singh <157497677+singhsayan@users.noreply.github.com> Date: Thu, 4 Sep 2025 08:57:12 +0200 Subject: [PATCH 5/9] Updated code --- dynamic_programming/unique_paths.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/unique_paths.cpp b/dynamic_programming/unique_paths.cpp index 35e9c34a21f..6a6d10665ae 100644 --- a/dynamic_programming/unique_paths.cpp +++ b/dynamic_programming/unique_paths.cpp @@ -88,7 +88,7 @@ class UniquePathsSolver { * @brief Self-test implementations */ static void test() { - using namespace dp::dynamic_programming; + using namespace dynamic_programming; UniquePathsSolver solver1(3, 7); assert(solver1.uniquePathsMemo() == 28); From 1a71f3b9861a545346d704b7735d372ec5e9cb54 Mon Sep 17 00:00:00 2001 From: Sayan Singh <157497677+singhsayan@users.noreply.github.com> Date: Thu, 4 Sep 2025 18:44:02 +0100 Subject: [PATCH 6/9] Updated_code --- dynamic_programming/unique_paths.cpp | 48 +++++++++++++++------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/dynamic_programming/unique_paths.cpp b/dynamic_programming/unique_paths.cpp index 6a6d10665ae..92ea5c548c0 100644 --- a/dynamic_programming/unique_paths.cpp +++ b/dynamic_programming/unique_paths.cpp @@ -1,12 +1,22 @@ /** * @file - * @brief Implementation of Unique Paths problem using - * Dynamic Programming (Memoization + Tabulation). + * @brief Implementation of Unique Paths problem using Dynamic Programming. * @details - * A robot is located at the top-left corner of an m x n grid. + * A robot is located at the top-left corner of an m ×ばつ n grid. * The robot can move either down or right at any point in time. - * The robot is trying to reach the bottom-right corner. - * This program computes the total number of unique paths. + * This program computes the total number of unique paths to reach + * the bottom-right corner. + * + * Approaches: + * - **Memoization (Top-Down)**: Recursively explores solutions while + * storing intermediate results in a cache (`dp`) to avoid redundant + * computation. Typically more intuitive and easy to write, but + * relies on recursion and has associated call-stack overhead. + * + * - **Tabulation (Bottom-Up)**: Iteratively builds the solution using + * a DP table, starting from the base cases and filling up the table. + * Offers consistent performance without recursion overhead and + * is generally more space-efficient when optimized. * * @see https://leetcode.com/problems/unique-paths/ */ @@ -30,21 +40,6 @@ class UniquePathsSolver { std::vector> dp; ///< Memoization table int m, n; - /** - * @brief Recursive + Memoization solution. - * @param i Current row index - * @param j Current column index - * @return int Number of unique paths from (i, j) to (m-1, n-1) - */ - int solveMem(int i, int j) { - if (i>= m || j>= n) return 0; - if (i == m - 1 && j == n - 1) return 1; - if (dp.at(i).at(j) != -1) return dp.at(i).at(j); - - dp.at(i).at(j) = solveMem(i + 1, j) + solveMem(i, j + 1); - return dp.at(i).at(j); - } - /** * @brief Bottom-up Tabulation solution. * @return int Number of unique paths from (0, 0) to (m-1, n-1) @@ -72,12 +67,19 @@ class UniquePathsSolver { } /** - * @brief Get number of unique paths using Memoization + * @brief Get number of unique paths using Memoization (Top-Down) */ - int uniquePathsMemo() { return solveMem(0, 0); } + int uniquePathsMemo(int i = 0, int j = 0) { + if (i>= m || j>= n) return 0; + if (i == m - 1 && j == n - 1) return 1; + if (dp.at(i).at(j) != -1) return dp.at(i).at(j); + + dp.at(i).at(j) = uniquePathsMemo(i + 1, j) + uniquePathsMemo(i, j + 1); + return dp.at(i).at(j); + } /** - * @brief Get number of unique paths using Tabulation + * @brief Get number of unique paths using Tabulation (Bottom-Up) */ int uniquePathsTab() { return solveTab(); } }; From 5e5b2cda56ecef937cac0a0fc30309003e099ebf Mon Sep 17 00:00:00 2001 From: Sayan Singh <157497677+singhsayan@users.noreply.github.com> Date: Fri, 5 Sep 2025 15:52:10 +0100 Subject: [PATCH 7/9] Update unique_paths.cpp --- dynamic_programming/unique_paths.cpp | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/dynamic_programming/unique_paths.cpp b/dynamic_programming/unique_paths.cpp index 92ea5c548c0..b4bcbd36136 100644 --- a/dynamic_programming/unique_paths.cpp +++ b/dynamic_programming/unique_paths.cpp @@ -9,7 +9,7 @@ * * Approaches: * - **Memoization (Top-Down)**: Recursively explores solutions while - * storing intermediate results in a cache (`dp`) to avoid redundant + * storing intermediate results in a cache (`memoization_table`) to avoid redundant * computation. Typically more intuitive and easy to write, but * relies on recursion and has associated call-stack overhead. * @@ -37,14 +37,14 @@ namespace dynamic_programming { */ class UniquePathsSolver { private: - std::vector> dp; ///< Memoization table + std::vector> memoization_table; ///< Memoization table int m, n; /** * @brief Bottom-up Tabulation solution. * @return int Number of unique paths from (0, 0) to (m-1, n-1) */ - int solveTab() { + int solveTabulation() { std::vector> table(m, std::vector(n, 0)); for (int i = 0; i < m; i++) table[i][n - 1] = 1; ///< last column @@ -60,10 +60,10 @@ class UniquePathsSolver { public: /** - * @brief Constructor initializes dimensions and memo table + * @brief Constructor initializes dimensions and memoization table */ UniquePathsSolver(int rows, int cols) : m(rows), n(cols) { - dp.assign(m, std::vector(n, -1)); + memoization_table.assign(m, std::vector(n, -1)); } /** @@ -72,16 +72,17 @@ class UniquePathsSolver { int uniquePathsMemo(int i = 0, int j = 0) { if (i>= m || j>= n) return 0; if (i == m - 1 && j == n - 1) return 1; - if (dp.at(i).at(j) != -1) return dp.at(i).at(j); + if (memoization_table.at(i).at(j) != -1) return memoization_table.at(i).at(j); - dp.at(i).at(j) = uniquePathsMemo(i + 1, j) + uniquePathsMemo(i, j + 1); - return dp.at(i).at(j); + memoization_table.at(i).at(j) = + uniquePathsMemo(i + 1, j) + uniquePathsMemo(i, j + 1); + return memoization_table.at(i).at(j); } /** * @brief Get number of unique paths using Tabulation (Bottom-Up) */ - int uniquePathsTab() { return solveTab(); } + int uniquePathsTabulation() { return solveTabulation(); } }; } // namespace dynamic_programming @@ -94,19 +95,19 @@ static void test() { UniquePathsSolver solver1(3, 7); assert(solver1.uniquePathsMemo() == 28); - assert(solver1.uniquePathsTab() == 28); + assert(solver1.uniquePathsTabulation() == 28); UniquePathsSolver solver2(3, 2); assert(solver2.uniquePathsMemo() == 3); - assert(solver2.uniquePathsTab() == 3); + assert(solver2.uniquePathsTabulation() == 3); UniquePathsSolver solver3(1, 1); assert(solver3.uniquePathsMemo() == 1); - assert(solver3.uniquePathsTab() == 1); + assert(solver3.uniquePathsTabulation() == 1); UniquePathsSolver solver4(2, 2); assert(solver4.uniquePathsMemo() == 2); - assert(solver4.uniquePathsTab() == 2); + assert(solver4.uniquePathsTabulation() == 2); std::cout << "All tests have successfully passed!\n"; } From d7f9c60517da41624e34f8158cdc3f2b8b013c51 Mon Sep 17 00:00:00 2001 From: Sayan Singh <157497677+singhsayan@users.noreply.github.com> Date: 2025年9月27日 08:21:33 +0100 Subject: [PATCH 8/9] Update unique_paths.cpp --- dynamic_programming/unique_paths.cpp | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/dynamic_programming/unique_paths.cpp b/dynamic_programming/unique_paths.cpp index b4bcbd36136..af2da673914 100644 --- a/dynamic_programming/unique_paths.cpp +++ b/dynamic_programming/unique_paths.cpp @@ -1,12 +1,15 @@ /** * @file - * @brief Implementation of Unique Paths problem using Dynamic Programming. + * @brief Implementation of Unique Paths (LeetCode 62) using Dynamic Programming. * @details * A robot is located at the top-left corner of an m ×ばつ n grid. * The robot can move either down or right at any point in time. * This program computes the total number of unique paths to reach * the bottom-right corner. * + * Note: This is **Unique Paths I** (no obstacles). Obstacles are handled in + * the separate problem **Unique Paths II**. + * * Approaches: * - **Memoization (Top-Down)**: Recursively explores solutions while * storing intermediate results in a cache (`memoization_table`) to avoid redundant @@ -37,8 +40,9 @@ namespace dynamic_programming { */ class UniquePathsSolver { private: - std::vector> memoization_table; ///< Memoization table - int m, n; + std::vector> memoization_table; ///< Memoization table to cache intermediate results + std::size_t m; ///< Number of rows in the grid + std::size_t n; ///< Number of columns in the grid /** * @brief Bottom-up Tabulation solution. @@ -47,11 +51,11 @@ class UniquePathsSolver { int solveTabulation() { std::vector> table(m, std::vector(n, 0)); - for (int i = 0; i < m; i++) table[i][n - 1] = 1; ///< last column - for (int j = 0; j < n; j++) table[m - 1][j] = 1; ///< last row + for (std::size_t i = 0; i < m; i++) table[i][n - 1] = 1; ///< last column + for (std::size_t j = 0; j < n; j++) table[m - 1][j] = 1; ///< last row - for (int i = m - 2; i>= 0; i--) { - for (int j = n - 2; j>= 0; j--) { + for (int i = static_cast(m) - 2; i>= 0; i--) { + for (int j = static_cast(n) - 2; j>= 0; j--) { table[i][j] = table[i + 1][j] + table[i][j + 1]; } } @@ -61,15 +65,20 @@ class UniquePathsSolver { public: /** * @brief Constructor initializes dimensions and memoization table + * @param rows number of rows in the grid (must be> 0) + * @param cols number of columns in the grid (must be> 0) */ - UniquePathsSolver(int rows, int cols) : m(rows), n(cols) { + UniquePathsSolver(std::size_t rows, std::size_t cols) : m(rows), n(cols) { memoization_table.assign(m, std::vector(n, -1)); } /** * @brief Get number of unique paths using Memoization (Top-Down) + * @param i current row index (default = 0) + * @param j current column index (default = 0) + * @return int Number of unique paths from (i, j) to (m-1, n-1) */ - int uniquePathsMemo(int i = 0, int j = 0) { + int uniquePathsMemo(std::size_t i = 0, std::size_t j = 0) { if (i>= m || j>= n) return 0; if (i == m - 1 && j == n - 1) return 1; if (memoization_table.at(i).at(j) != -1) return memoization_table.at(i).at(j); @@ -81,6 +90,7 @@ class UniquePathsSolver { /** * @brief Get number of unique paths using Tabulation (Bottom-Up) + * @return int Number of unique paths from (0, 0) to (m-1, n-1) */ int uniquePathsTabulation() { return solveTabulation(); } }; From df740a056745636656aa14532ab347c3bf2dc8c6 Mon Sep 17 00:00:00 2001 From: Sayan Singh <157497677+singhsayan@users.noreply.github.com> Date: 2025年9月27日 12:03:43 +0100 Subject: [PATCH 9/9] Update unique_paths.cpp --- dynamic_programming/unique_paths.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/dynamic_programming/unique_paths.cpp b/dynamic_programming/unique_paths.cpp index af2da673914..d5e89b34c8b 100644 --- a/dynamic_programming/unique_paths.cpp +++ b/dynamic_programming/unique_paths.cpp @@ -40,7 +40,7 @@ namespace dynamic_programming { */ class UniquePathsSolver { private: - std::vector> memoization_table; ///< Memoization table to cache intermediate results + std::vector> memoization_table; ///< Memoization table to cache intermediate results (-1 = uncomputed) std::size_t m; ///< Number of rows in the grid std::size_t n; ///< Number of columns in the grid @@ -51,11 +51,13 @@ class UniquePathsSolver { int solveTabulation() { std::vector> table(m, std::vector(n, 0)); - for (std::size_t i = 0; i < m; i++) table[i][n - 1] = 1; ///< last column - for (std::size_t j = 0; j < n; j++) table[m - 1][j] = 1; ///< last row + // base cases: last column and last row + for (std::size_t i = 0; i < m; i++) table[i][n - 1] = 1; + for (std::size_t j = 0; j < n; j++) table[m - 1][j] = 1; - for (int i = static_cast(m) - 2; i>= 0; i--) { - for (int j = static_cast(n) - 2; j>= 0; j--) { + // fill the table from bottom-right to top-left + for (std::size_t i = m - 1; i--> 0;) { + for (std::size_t j = n - 1; j--> 0;) { table[i][j] = table[i + 1][j] + table[i][j + 1]; } }

AltStyle によって変換されたページ (->オリジナル) /