From 86a92dc84809ae90432793e334211b8b67158dd6 Mon Sep 17 00:00:00 2001 From: Kaushik Narayan R Date: Mon, 9 Oct 2023 09:13:59 -0700 Subject: [PATCH] day 12: 62-unique-paths --- 62-unique-paths/driver.cpp | 10 ++++++++++ 62-unique-paths/soln.cpp | 34 ++++++++++++++++++++++++++++++++++ 62-unique-paths/soln.hpp | 8 ++++++++ 3 files changed, 52 insertions(+) create mode 100644 62-unique-paths/driver.cpp create mode 100644 62-unique-paths/soln.cpp create mode 100644 62-unique-paths/soln.hpp diff --git a/62-unique-paths/driver.cpp b/62-unique-paths/driver.cpp new file mode 100644 index 0000000..9e124b4 --- /dev/null +++ b/62-unique-paths/driver.cpp @@ -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; +} \ No newline at end of file diff --git a/62-unique-paths/soln.cpp b/62-unique-paths/soln.cpp new file mode 100644 index 0000000..793d187 --- /dev/null +++ b/62-unique-paths/soln.cpp @@ -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; +} \ No newline at end of file diff --git a/62-unique-paths/soln.hpp b/62-unique-paths/soln.hpp new file mode 100644 index 0000000..f5ff9b4 --- /dev/null +++ b/62-unique-paths/soln.hpp @@ -0,0 +1,8 @@ +#include +using namespace std; +class Solution +{ +public: + int uniquePaths(int m, int n); + bool test(int m, int n, int answer); +}; \ No newline at end of file