-
-
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?
Conversation
Hi @Panquesito7 @realstealthninja |
dynamic_programming/unique_paths.cpp
Outdated
#include <iostream> | ||
#include <vector> | ||
#include <cassert> | ||
using namespace std; |
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.
using namespace std; |
dynamic_programming/unique_paths.cpp
Outdated
* @namespace dp | ||
* @brief Dynamic Programming algorithms | ||
*/ | ||
namespace dp { |
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.
namespace dp { | |
namespace dynamic_programming { |
dynamic_programming/unique_paths.cpp
Outdated
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); |
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.
dp[i][j] = solveMem(i + 1, j, m, n, dp) + solveMem(i, j + 1, m, n, dp); | |
dp.at(i).at(j) = solveMem(i + 1, j, m, n, dp) + solveMem(i, j + 1, m, n, dp); |
dynamic_programming/unique_paths.cpp
Outdated
* @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<vector<int>> &dp) { |
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.
this is bad design, the user should not have to worry about supplying a memoization table, This is design details that should be handled internally
dynamic_programming/unique_paths.cpp
Outdated
* @brief Self-test implementations | ||
*/ | ||
static void test() { | ||
assert(dp::uniquePaths(3, 7) == 28); |
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.
these tests only test the tabular form and not the memoized form
dynamic_programming/unique_paths.cpp
Outdated
* @return int Total number of unique paths | ||
*/ | ||
int uniquePaths(int m, int n) { | ||
vector<vector<int>> dp(m + 1, vector<int>(n + 1, -1)); |
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.
Maybe this should be turned into a class? with a private member of dp, and multiple behaviours, solvetabular() or solvememoised()
Hi @realstealthninja, I've made the requested changes. Could you please review again ? Thanks ! |
* 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. |
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?
dynamic_programming/unique_paths.cpp
Outdated
/** | ||
* @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 comment
The 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?
@realstealthninja Could you please review the latest changes and let me know if everything looks fine? Thanks for your time! |
dynamic_programming/unique_paths.cpp
Outdated
*/ | ||
class UniquePathsSolver { | ||
private: | ||
std::vector<std::vector<int>> dp; ///< Memoization table |
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.
this variable name is too short, its nondescript/confusing. maybe rename it to memoization_table or something similar
dynamic_programming/unique_paths.cpp
Outdated
/** | ||
* @brief Get number of unique paths using Tabulation (Bottom-Up) | ||
*/ | ||
int uniquePathsTab() { return solveTab(); } |
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.
you could do the same thing for this function as well
@realstealthninja Done |
Hi @realstealthninja, can you take a quick look at my PR and merge if it looks fine? |
Added Unique Paths algorithm using Dynamic Programming (unique_paths.cpp) with both memoization and tabulation approaches. Includes self-tests with assert().
Type of Change
• New feature
Tests
• Verified with multiple test cases (3x7, 3x2, 1x1, 2x2)
• All tests passed
Checklist
• Code compiles locally
• Tests included and passing
• Naming conventions followed
• Doxygen-style comments added