mirror of
https://github.com/20kaushik02/leetcode-gulag.git
synced 2025-12-06 06:24:07 +00:00
day 12: 62-unique-paths
This commit is contained in:
parent
107a4c624f
commit
86a92dc848
10
62-unique-paths/driver.cpp
Normal file
10
62-unique-paths/driver.cpp
Normal 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
34
62-unique-paths/soln.cpp
Normal 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
8
62-unique-paths/soln.hpp
Normal 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);
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user