forked from ssitu001/toyProblemPractice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlatticePaths.js
34 lines (31 loc) · 919 Bytes
/
latticePaths.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*
* Problem: Lattice Paths
*
* Prompt: Count the number of unique paths to travel from the top left
* corder to the bottom right corner of a lattice of M x N squares.
*
* When moving through the lattice, one can only travel to the adjacent
* corner on the right or down.
*
* Input: m {Integer} - rows of squares
* Input: n {Integer} - column of squares
* Output: {Integer}
*
* Example: input: (2, 3)
*
* (2 x 3 lattice of squares)
* __ __ __
* |__|__|__|
* |__|__|__|
*
* output: 10 (number of unique paths from top left corner to bottom right)
*
* Resource: https://projecteuler.net/problem=15
*
*/
const latticePaths = (rows, cols) => {
if (rows < 0 || cols < 0) return 0;
if (rows === 0 && cols === 0) return 1;
return latticePaths(rows-1, cols) + latticePaths(rows, cols-1);
}
console.log(latticePaths(2, 3));