Skip to content

Commit 34f666e

Browse files
committed
Prob 164: Number of unique paths in a grid
1 parent 9a9ac4f commit 34f666e

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
| Current Status| Stats |
88
| :------------: | :----------: |
9-
| Total Problems | 163 |
9+
| Total Problems | 164 |
1010

1111
</center>
1212

@@ -103,6 +103,8 @@ Include contains single header implementation of data structures and some algori
103103
| Longest Common Subsequence Problem | [lcs.cpp](dynamic_programming_problems/lcs.cpp) |
104104
| Maximum Value Contigous Subsequence Problem [wiki](https://en.wikipedia.org/wiki/Maximum_subarray_problem)| [max_subsequence.cpp](dynamic_programming_problems/max_subsequence.cpp)|
105105
| Catalan number - Count the number of possible Binary Search Trees with n keys | [catalan_number.cpp](dynamic_programming_problems/catalan_number.cpp)|
106+
| Calculate the number of unique paths from source origin (0, 0) to destination (m-1, n-1) in a m x n grid. You can only move either in down or right direction.|
107+
[unique_paths.cpp](dynamic_programming_problems/unique_paths.cpp)|
106108

107109
### Tree Problems
108110
| Problem | Solution |
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Level : Easy
3+
* Given a grid, calculate the maximum number of unique paths
4+
* from source (0,0) to destination (m, n). You can only move
5+
* either in down or in right direction.
6+
*
7+
* S . . . . . . . .
8+
* . . . . . . . . .
9+
* . . . . . . . . .
10+
* . . . . . . . . .
11+
* . . . . . . . . .
12+
* . . . . . . . . D
13+
*
14+
* In this 6 x 9 grid,
15+
* unique paths from source origin to destination are 1287.
16+
*
17+
*/
18+
19+
#include <iostream>
20+
#include <vector>
21+
22+
int unique_paths(int m, int n)
23+
{
24+
std::vector<std::vector<int>> paths(m, std::vector<int>(n));
25+
for (int i = 0; i < m; ++i) {
26+
paths[i][0] = 1;
27+
}
28+
29+
for (int j = 0; j < n; ++j) {
30+
paths[0][j] = 1;
31+
}
32+
33+
for (int i = 1; i < m; ++i) {
34+
for (int j = 1; j < n; ++j) {
35+
paths[i][j] = paths[i-1][j] + paths[i][j-1];
36+
}
37+
}
38+
return paths[m-1][n-1];
39+
}
40+
41+
int main()
42+
{
43+
std::cout << "Number of unique paths for 6 x 9 grid: " <<
44+
unique_paths(6, 9) << std::endl;
45+
return 0;
46+
}

0 commit comments

Comments
 (0)