-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path015.js
More file actions
31 lines (26 loc) · 698 Bytes
/
015.js
File metadata and controls
31 lines (26 loc) · 698 Bytes
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
// Starting in the top left corner of a 2×2 grid, and only being able to move to
// the right and down, there are exactly 6 routes to the bottom right corner.
// How many such routes are there through a 20×20 grid?
var latticePaths = function() {
var SIZE = 20;
var grid = Array(SIZE);
for (var i = 0; i <= SIZE; i++) {
grid[i] = Array(SIZE);
}
return function(x, y) {
var paths = 0;
if (x === 0 || y === 0) {
return 1;
}
if (x > SIZE || y > SIZE) {
return -1;
}
if (typeof grid[x][y] === 'number') {
return grid[x][y];
}
paths = latticePaths(x-1, y) + latticePaths(x, y-1);
grid[x][y] = paths;
return paths;
};
}();
console.log( latticePaths(20, 20) );