Skip to content

Commit b73ddec

Browse files
committed
Add Pascal's Triangle based solution for Unique Paths problem.
1 parent d8fb657 commit b73ddec

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ a set of rules that precisely define a sequence of operations.
118118
*`B`[Tower of Hanoi](src/algorithms/uncategorized/hanoi-tower)
119119
*`B`[Square Matrix Rotation](src/algorithms/uncategorized/square-matrix-rotation) - in-place algorithm
120120
*`B`[Jump Game](src/algorithms/uncategorized/jump-game) - backtracking, dynamic programming (top-down + bottom-up) and greedy examples
121-
*`B`[Unique Paths](src/algorithms/uncategorized/unique-paths) - backtracking and dynamic programming examples
121+
*`B`[Unique Paths](src/algorithms/uncategorized/unique-paths) - backtracking, dynamic programming and Pascal's Triangle based examples
122122
*`A`[N-Queens Problem](src/algorithms/uncategorized/n-queens)
123123
*`A`[Knight's Tour](src/algorithms/uncategorized/knight-tour)
124124

‎src/algorithms/uncategorized/unique-paths/README.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ the bottom right one with number `3`.
9494

9595
**Auxiliary Space Complexity**: `O(m * n)` - since we need to have DP matrix.
9696

97+
### Pascal's Triangle Based
98+
99+
This question is actually another form of Pascal Triangle.
100+
101+
The corner of this rectangle is at `m + n - 2` line, and
102+
at `min(m, n) - 1` position of the Pascal's Triangle.
103+
97104
## References
98105

99106
-[LeetCode](https://leetcode.com/problems/unique-paths/description/)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
importuniquePathsfrom'../uniquePaths';
2+
3+
describe('uniquePaths',()=>{
4+
it('should find the number of unique paths on board',()=>{
5+
expect(uniquePaths(3,2)).toBe(3);
6+
expect(uniquePaths(7,3)).toBe(28);
7+
expect(uniquePaths(3,7)).toBe(28);
8+
expect(uniquePaths(10,10)).toBe(48620);
9+
expect(uniquePaths(100,1)).toBe(1);
10+
expect(uniquePaths(1,100)).toBe(1);
11+
});
12+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
importpascalTrianglefrom'../../math/pascal-triangle/pascalTriangle';
2+
3+
/**
4+
* @param{number} width
5+
* @param{number} height
6+
* @return{number}
7+
*/
8+
exportdefaultfunctionuniquePaths(width,height){
9+
constpascalLine=width+height-2;
10+
constpascalLinePosition=Math.min(width,height)-1;
11+
12+
returnpascalTriangle(pascalLine)[pascalLinePosition];
13+
}

0 commit comments

Comments
(0)