Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Open
singhsayan wants to merge 8 commits into TheAlgorithms:master
base: master
Choose a base branch
Loading
from singhsayan:feat/grid_unique_path

Conversation

Copy link

@singhsayan singhsayan commented Sep 2, 2025

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

Copy link
Author

Hi @Panquesito7 @realstealthninja
I’ve added the Unique Paths solution with both memoization and tabulation, including self-tests. Could you please review when you get a chance? Thanks!

#include <iostream>
#include <vector>
#include <cassert>
using namespace std;
Copy link
Collaborator

@realstealthninja realstealthninja Sep 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
using namespace std;

* @namespace dp
* @brief Dynamic Programming algorithms
*/
namespace dp {
Copy link
Collaborator

@realstealthninja realstealthninja Sep 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
namespace dp {
namespace dynamic_programming {

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);
Copy link
Collaborator

@realstealthninja realstealthninja Sep 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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);

* @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) {
Copy link
Collaborator

@realstealthninja realstealthninja Sep 4, 2025

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

* @brief Self-test implementations
*/
static void test() {
assert(dp::uniquePaths(3, 7) == 28);
Copy link
Collaborator

@realstealthninja realstealthninja Sep 4, 2025

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

* @return int Total number of unique paths
*/
int uniquePaths(int m, int n) {
vector<vector<int>> dp(m + 1, vector<int>(n + 1, -1));
Copy link
Collaborator

@realstealthninja realstealthninja Sep 4, 2025

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()

Copy link
Author

Hi @realstealthninja, I've made the requested changes. Could you please review again ? Thanks !

realstealthninja reacted with thumbs up emoji

* 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.
Copy link
Collaborator

@realstealthninja realstealthninja Sep 4, 2025

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?

/**
* @brief Get number of unique paths using Memoization
*/
int uniquePathsMemo() { return solveMem(0, 0); }
Copy link
Collaborator

@realstealthninja realstealthninja Sep 4, 2025

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?

Copy link
Author

@realstealthninja Could you please review the latest changes and let me know if everything looks fine? Thanks for your time!

*/
class UniquePathsSolver {
private:
std::vector<std::vector<int>> dp; ///< Memoization table
Copy link
Collaborator

@realstealthninja realstealthninja Sep 5, 2025

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

/**
* @brief Get number of unique paths using Tabulation (Bottom-Up)
*/
int uniquePathsTab() { return solveTab(); }
Copy link
Collaborator

@realstealthninja realstealthninja Sep 5, 2025

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

Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers

@realstealthninja realstealthninja realstealthninja requested changes

@Panquesito7 Panquesito7 Awaiting requested review from Panquesito7 Panquesito7 is a code owner

Requested changes must be addressed to merge this pull request.

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

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