File tree 4 files changed +33
-1
lines changed
src/algorithms/uncategorized/unique-paths
4 files changed +33
-1
lines changed Original file line number Diff line number Diff line change @@ -118,7 +118,7 @@ a set of rules that precisely define a sequence of operations.
118
118
* ` B ` [ Tower of Hanoi] ( src/algorithms/uncategorized/hanoi-tower )
119
119
* ` B ` [ Square Matrix Rotation] ( src/algorithms/uncategorized/square-matrix-rotation ) - in-place algorithm
120
120
* ` 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
122
122
* ` A ` [ N-Queens Problem] ( src/algorithms/uncategorized/n-queens )
123
123
* ` A ` [ Knight's Tour] ( src/algorithms/uncategorized/knight-tour )
124
124
Original file line number Diff line number Diff line change @@ -94,6 +94,13 @@ the bottom right one with number `3`.
94
94
95
95
** Auxiliary Space Complexity** : ` O(m * n) ` - since we need to have DP matrix.
96
96
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
+
97
104
## References
98
105
99
106
- [ LeetCode] ( https://leetcode.com/problems/unique-paths/description/ )
Original file line number Diff line number Diff line change
1
+ import uniquePaths from '../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
+ } ) ;
Original file line number Diff line number Diff line change
1
+ import pascalTriangle from '../../math/pascal-triangle/pascalTriangle' ;
2
+
3
+ /**
4
+ * @param {number } width
5
+ * @param {number } height
6
+ * @return {number }
7
+ */
8
+ export default function uniquePaths ( width , height ) {
9
+ const pascalLine = width + height - 2 ;
10
+ const pascalLinePosition = Math . min ( width , height ) - 1 ;
11
+
12
+ return pascalTriangle ( pascalLine ) [ pascalLinePosition ] ;
13
+ }
You can’t perform that action at this time.
0 commit comments