Skip to content

Conversation

singhsayan
Copy link

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

@singhsayan
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

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

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

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

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

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

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

@singhsayan
Copy link
Author

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

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

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?

@singhsayan
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

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

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

@singhsayan
Copy link
Author

@realstealthninja Done

@singhsayan
Copy link
Author

Hi @realstealthninja, can you take a quick look at my PR and merge if it looks fine?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants