day 12: 62-unique-paths

This commit is contained in:
Kaushik Narayan R 2023-10-09 09:13:59 -07:00
parent 107a4c624f
commit 86a92dc848
3 changed files with 52 additions and 0 deletions

View File

@ -0,0 +1,10 @@
#include "soln.hpp"
int main()
{
int m = 3, n = 7;
int answer = 28;
Solution soln;
cout << "Found no. of paths correctly? " << soln.test(m, n, answer) << endl;
return 0;
}

34
62-unique-paths/soln.cpp Normal file
View File

@ -0,0 +1,34 @@
#include "soln.hpp"
int Solution::uniquePaths(int m, int n)
{
int grid[m][n];
// initial condition
grid[0][0] = 1;
// first row all else 1s: only right moves
for (int i = 1; i < n; i++)
{
grid[0][i] = 1;
}
// first column all 1s: only down moves
for (int j = 1; j < m; j++)
{
grid[j][0] = 1;
}
// no. of paths to a cell = sum of no. of paths to left and top cells
for (int i = 1; i < m; i++)
{
for (int j = 1; j < n; j++)
{
grid[i][j] = grid[i - 1][j] + grid[i][j - 1];
}
}
return grid[m - 1][n - 1];
}
bool Solution::test(int m, int n, int answer)
{
return uniquePaths(m, n) == answer;
}

8
62-unique-paths/soln.hpp Normal file
View File

@ -0,0 +1,8 @@
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
int uniquePaths(int m, int n);
bool test(int m, int n, int answer);
};