-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Adding dynamic programming solution for unique paths problem #2992
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 5 commits
a73323e
ba22f9f
d87ca7c
8dce8c6
548c500
1a71f3b
eb39769
5e5b2cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/** | ||
* @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 <iostream> | ||
#include <vector> | ||
#include <cassert> | ||
|
||
/** | ||
* @namespace dynamic_programming | ||
* @brief Dynamic Programming algorithms | ||
*/ | ||
namespace dynamic_programming { | ||
|
||
/** | ||
* @class UniquePathsSolver | ||
* @brief Solves the Unique Paths problem using both memoization and tabulation. | ||
*/ | ||
class UniquePathsSolver { | ||
private: | ||
std::vector<std::vector<int>> dp; ///< Memoization table | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this variable name is too short, its nondescript/confusing. maybe rename it to memoization_table or something similar |
||
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) | ||
*/ | ||
int solveTab() { | ||
std::vector<std::vector<int>> table(m, std::vector<int>(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]; | ||
} | ||
|
||
public: | ||
/** | ||
* @brief Constructor initializes dimensions and memo table | ||
*/ | ||
UniquePathsSolver(int rows, int cols) : m(rows), n(cols) { | ||
dp.assign(m, std::vector<int>(n, -1)); | ||
} | ||
|
||
/** | ||
* @brief Get number of unique paths using Memoization | ||
*/ | ||
int uniquePathsMemo() { return solveMem(0, 0); } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oslve mem isnt used anywhere why not replace the uniqupath memo function definitino with the one with solvemem and remove solvemem entirely? |
||
|
||
/** | ||
* @brief Get number of unique paths using Tabulation | ||
*/ | ||
int uniquePathsTab() { return solveTab(); } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you could do the same thing for this function as well |
||
}; | ||
|
||
} // namespace dynamic_programming | ||
|
||
/** | ||
* @brief Self-test implementations | ||
*/ | ||
static void test() { | ||
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"; | ||
} | ||
|
||
/** | ||
* @brief Main function | ||
* @returns 0 on successful execution | ||
*/ | ||
int main() { | ||
test(); // run self-tests | ||
return 0; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could add a brief comparison between memoization and tabulation?